I would like to use a custom directive that binds to an object, but I would like to specify the field used in the template. Previously, I was using {{item.Name}} but I would like to bind to any object, specifying the display field.
This is what I have
var foo = function () {
return {
restrict: 'E',
scope: {
items: '='
},
template:
"<div class='holder'>"
+ "<a data-ng-repeat='item in items' data-ng-click='myClick(item)'><i class='fa fa-times'/>{{item.Name}}</a>"
+ "</div>",
controller: function ($scope) {......}
}
}
I'd like to do this:
var foo = function () {
return {
restrict: 'E',
scope: {
items: '=',
display_field: 'Name',
icon_field: 'fa fa-times',
},
template:
"<div class='holder'>"
+ "<a data-ng-repeat='item in items' data-ng-click='myClick(item)'><i data-ng-class='{{item.icon_field}}'/>{{item.display_field}}</a>"
+ "</div>",
controller: function ($scope) {......}
}
}
Where the display_field and icon can be specified like this:
<foo items="myItems" display_field="OtherProperty" icon-field="iconProperty" />
fiddle: http://jsfiddle.net/1L7tdd1p/