这里是一个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