我有来自 Angular 智能表的指令,用于将复选框选择添加到如下所示的表的行中。我希望在单击复选框时能够执行自定义函数。这可能吗 ?我基本上需要在单击复选框时存储 rowid 或行的其他属性。
app.directive('csSelect', function () {
return {
require: '^stTable',
template: '<input type="checkbox"/>',
scope: {
row: '=csSelect'
},
link: function (scope, element, attr, ctrl) {
element.bind('change', function (evt) {
scope.$apply(function () {
ctrl.select(scope.row, 'multiple');
});
});
scope.$watch('row.isSelected', function (newValue, oldValue) {
if (newValue === true) {
element.parent().addClass('st-selected');
} else {
element.parent().removeClass('st-selected');
}
});
}
};
});
编辑
该指令的使用方式如下:
<td cs-select="row"></td>
请注意,如果我将 ng-click 添加到 td
元素来检查单击的行,我是否单击复选框并不重要。一旦我单击 td 元素内的任意位置,它就会像我单击该行一样起作用。所以这不是一个解决方案
最佳答案
app.directive('csSelect', function () {
return {
require: '^stTable',
template: '<input type="checkbox"/>',
scope: {
row: '=csSelect',
myFunc: '&cs-func'
},
link: function (scope, element, attr, ctrl) {
element.bind('change', function (evt) {
scope.$apply(function () {
ctrl.select(scope.row, 'multiple');
});
});
scope.$watch('row.isSelected', function (newValue, oldValue) {
if (newValue === true) {
element.parent().addClass('st-selected');
scope.myFunc();
} else {
element.parent().removeClass('st-selected');
}
});
}
};
});
然后
<td cs-select="row" cs-func='alert("hello")'></td>
关于javascript - 如何在 angularjs 指令中执行自定义函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33962673/