這裏是一個TypeScript示例如何調用從嵌入式指令返回控制器上的方法。最重要的一點是,回調的指令參數名稱在定義時使用&,並且在調用該回調函數時,不應使用位置參數,而應使用具有目標中參數名稱的屬性的對象。
當你創建你的應用程序模塊註冊指令:
module MyApp {
var app: angular.IModule = angular.module("MyApp");
MyApp.Directives.FileUploader.register(app);
}
登記代碼如下:
該指令的控制器是這樣
module MyApp.Directives.FileUploader {
export class Controller {
public files: string[] = ["One", "Two", "Three"];
//The callback specified in the view that created this directive instance
public onFileItemClicked: (fileItem) => void;
// This is the controller method called from its HTML's ng-click
public fileItemClicked(fileItem) {
//IMPORTANT: Don't use comma separated parameters,
//instead use an object with property names to act as named parameters
this.onFileItemClicked({
fileItem: fileItem
});
}
}
}
的指令的HTML看起來像這樣
<ul>
<li ng-repeat="item in controller.files" ng-click="controller.fileItemClicked (item)">
{{ item }}
</li>
</ul>
主要看有你自己的指令的一個實例,像這樣
<body ng-app="MyApp" ng-controller="MainController as controller">
<file-uploader on-file-item-clicked="controller.fileItemClicked(fileItem)"/>
</body>
現在,所有你需要在你的MainController是一種方法
public fileItemClicked(fileItem) {
alert("Clicked " + fileItem);
}
你已經在使用的指令分離範圍根據文章,控制器 – 2013-04-24 18:52:06