Javascript Angular js指令使用控制器作为语法ng单击不工作
在Angular 1.3中使用“controller as”语法时,我很难在具有独立作用域的指令中获得单击事件。该指令的代码如下:Javascript Angular js指令使用控制器作为语法ng单击不工作,javascript,angularjs,directive,Javascript,Angularjs,Directive,在Angular 1.3中使用“controller as”语法时,我很难在具有独立作用域的指令中获得单击事件。该指令的代码如下: myDirectives.directive('gsphotosquare', dirfxn); function dirfxn() { var directive = { replace: false, scope: { photoInfo: '=', photoBatchN
myDirectives.directive('gsphotosquare', dirfxn);
function dirfxn() {
var directive = {
replace: false,
scope: {
photoInfo: '=',
photoBatchNum: '=',
thumbnailwidth: '='
},
restrict: 'EA',
controller: Ctrller,
controllerAs: 'ctrl',
template: '<div ng-click="ctrl.squareClicked()">test</div>',
//templateUrl: 'views/directives/gsphotosquare.html',
bindToController: true, // because the scope is isolated
link: linkFunc //adding this didn't help
};
return directive;
}
function Ctrller() {
var vm = this;
vm.squareClicked = function () {
alert('inside clickhandler for gsphotosquare directive');
};
}
function linkFunc(scope, el, attr, ctrl) {
el.bind('click', function () {
alert('inside clickhandler for gsphotosquare directive');
});
}
myDirectives.directive('gsphotosquare',dirfxn);
函数dirfxn(){
var指令={
替换:false,
范围:{
photoInfo:“=”,
photoBatchNum:“=”,
缩略图宽度:'='
},
限制:“EA”,
控制员:Ctrller,
controllerAs:'ctrl',
模板:“测试”,
//templateUrl:“视图/指令/gsphotosquare.html”,
bindToController:true,//因为作用域是隔离的
link:linkFunc//添加此项没有帮助
};
返回指令;
}
函数Ctrller(){
var vm=这个;
vm.squareClicked=函数(){
警报(“gsphotosquare指令的内部clickhandler”);
};
}
函数linkFunc(范围、el、属性、ctrl){
el.bind('单击',函数(){
警报(“gsphotosquare指令的内部clickhandler”);
});
}
下面是该指令在DOM中的使用方式:
<span class="input-label">General Site Photos</span>
<div class=" item row">
<gsphotosquare photo-info="mt.photos.v1f1[0]" photo-batch-num="mt.photoBatchNum" ></gsphotosquare>
<gsphotosquare photo-info="mt.photos.v1f1[1]" photo-batch-num="mt.photoBatchNum" ></gsphotosquare>
<gsphotosquare photo-info="mt.photos.v1f1[2]" photo-batch-num="mt.photoBatchNum" ></gsphotosquare>
<gsphotosquare photo-info="mt.photos.v1f1[3]" photo-batch-num="mt.photoBatchNum" ></gsphotosquare>
</div>
一般站点照片
您知道为什么单击呈现指令时不显示警报吗?尝试以稍微不同的方式定义控制器:
myDirectives.controller('Ctrller', Ctrller);
然后在你的指令中:
controller: 'Ctrller as ctrl',
在这里工作很好charlietfl,谢谢你的评论,这让我看到了正确的方向。显然,我把它深埋在一个表单中,并用标签元素(使用离子)将其包裹起来。只需从label元素中删除指令。如果你能重复你的评论作为回答,我会接受的。