【发布时间】:2025-12-20 13:55:12
【问题描述】:
我对将'&' 与隔离范围的rowClick 一起使用感到困惑。看看下面的代码。在一次代码审查中,我被告知这是一种反模式,应该使用'&',但我没有得到如何做到这一点的示例。
<div ng-controller="tableController as table">
<row ng-repeat="row in table.rows" row-data="row" row-click="table.updateChart">
</row>
</div>
.controller('tableController', [function() {
this.rows = [
{ id: 'foobar', values: ['Chris', 'Kayti'] }
];
this.updateChart = function(row) {
alert('TODO: update row ' + row.id);
};
}])
.directive('row', [function() {
return {
restrict: 'E',
scope: {
rowData: '=',
rowClick: '='
},
template: '<div class="row" ng-click="rowClick(rowData)">' +
'<span ng-repeat="cell in rowData.values">{{ cell }}</span>' +
'</div>'
};
}])
(见http://plnkr.co/edit/ZofcUQcPKQp4zeSNvoOe?p=preview)
我正在阅读它,但仍然感到困惑。有人可以给我一个示例,说明如何将这段代码重构为使用'&',好吗?
【问题讨论】:
-
以下这些答案有什么好运气吗?
标签: angularjs angularjs-directive isolate-scope