这里是我的指令:通功能,以指令
module app.directives {
interface IDmDeleteIconController {
doAction(): void;
}
class DmDeleteIconController implements IDmDeleteIconController {
static $inject = ['$scope'];
constructor(public $scope:any) {
}
doAction() {
console.log('1');
this.$scope.dmAction();
}
}
export class DmDeleteIcon implements ng.IDirective {
templateUrl = './app/common/directives/deleteIcon/deleteIcon.html';
controller = DmDeleteIconController;
controllerAs = 'dmDeleteIconVm';
scope = {
dmAction: '&'
};
link = (scope: any, element: ng.IAugmentedJQuery, attrs: ng.IAttributes, ctrl: any) => {
console.log('2');
scope.dmAction();
}
static factory(): ng.IDirectiveFactory {
const directive =() => new DmDeleteIcon();
directive.$inject = [];
return directive;
}
}
angular.module('myApp').directive('dmDeleteIcon', DmDeleteIcon.factory());
}
在这里,我尝试使用它:
当我打开网页,我会在控制台中2
(从link
函数),但我没有从我传递给指令的函数中获得hello
。
为什么以及如何解决?
更新:
这里是一个指令的模板:
a.item-detail-delete-icon(class="form-image-link" href="" ng-click="dmDeleteIconVm.doAction()")
这里是HTML到我的玉编译:
<dm-delete-icon dm-action="console.log('hello');"></dm-delete-icon>
更新2:
我试图用这样的:
<dm-delete-icon dm-action="vm.foo()"></dm-delete-icon>
其中:
foo(): void {
console.log("hello");
}
控制器的功能。
更新3:
如果我试图调试代码,我会得到这样的:
您是否可以在整个HTML标签中使用它? – toskv
你是指我从Jade那里得到的指令模板或HTML吗?我已将它们都添加到我的问题中 – demas
您没有为属性“dm-action”提供函数。 'console.log'不是一个函数,所以之后应该如何用'dmAction()'调用它。 – FlorianTopf