Angular js指令使用控制器作为语法ng点击不起作用
Angular js directive using controller as syntax ng-click not working
我在Angular 1.3中使用"controller as"语法在具有隔离作用域的指令中获取点击事件时遇到问题。指令代码如下:
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');
});
}
以下是该指令在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',
相关文章:
- 筛选ng选项时语法不正确
- Angular js指令使用控制器作为语法ng点击不起作用
- Ng类条件语法
- ng单击错误:[$parse:syntax]语法错误:令牌'对象'意外,应为[]
- 角度 - ng 网格通过分组按语法选择下一个/上一个项目
- 使用 ng 样式进行背景渐变时出现 AngularJS 语法错误
- 错误:使用 ng 类时出现 [$parse:语法]
- 在AngularJS中使用ng-change和“Controller As”语法
- 在ng-options中,是否可以使用“(索引,项目)在vm.items”语法中进行预选
- Angular JS中ng-if表达式中条件单向绑定的语法
- ng类的angularjs语法错误
- Angular ng模型语法不起作用
- Angularjs的ng类表达式语法
- Angular ng-click给出语法错误:Token '{'表达式第22列处的键无效
- 只有在迭代JSON对象时,键才使用更简洁的ng-repeat语法
- 使用ng-submit和'controllers '语法
- ng类中的连字符's对象键在编译过程中导致语法错误..但不会打碎任何东西
- 语法错误:ng messages='{{字段}}$错误'
- ng repeat-在控制器中更改数组时不更新视图,使用控制器作为语法
- AngularJS表达式在ng-click中引发语法错误