I've a directive as follows:
<selectable-item-list items="model.items">
<item-template>
<span ng-bind="item.text"></span>
</item-template>
</selectable-item-list>
The issue is in the <item-template>
, where item
would be a reference of currently iterated item when an internal ng-repeat
is bound inside <selectable-item-list>
.
AFAIK, it seems like transclusions can't see directive's scope, thus, that item.text
can't be bound because there's no item
at all.
How would you solve this scenario? Previously I was manually-transcluding <item-template>
but that other approach had other downsides/issues.
Here's a runnable code snippet that works as sample of my real-world case:
var app = angular.module("app", []);
app.controller("some", function() {
this.items = [{
text: "hello"
}, {
text: "bye"
}];
});
app.directive("test", function() {
return {
template: `<ol>
<li ng-repeat="item in items">
<div ng-transclude="itemTemplate"></div>
</li>
</ol>`,
transclude: {
"itemTemplate": "itemTemplate"
},
scope: {
"items": "="
}
}
});
<script src="https://code.angularjs.org/1.5.7/angular.js"></script>
<div ng-app="app" ng-controller="some as some">
<test items="some.items">
<item-template>
<span ng-bind="item.text"></span>
</item-template>
</test>
</div>