I'm confused about using '&'
with the isolate scope's rowClick
. Take a look at the code below. In a code review I was told that this, below, is an anti-pattern and that '&'
should be used instead, but I didn't get an example of how to do that.
<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>'
};
}])
(See http://plnkr.co/edit/ZofcUQcPKQp4zeSNvoOe?p=preview)
I'm reading about it but still confused. Can someone give me an example of how this code could be refactored to use '&'
, please?
It is a weird syntax and probably not well documented on how to pass the arguments. You would need to make some changes:-
In the template set
&
binding and pass the argument as value of the key with a name, sayrow
i.eng-click="rowClick({row: rowData})">
:When consuming the directive set up the name of the argument which should be inline with what your directive defines internally, i.e say
row
:Demo