【发布时间】:2016-02-21 22:39:07
【问题描述】:
我在 ng-repeat 中的自定义指令有问题。我试图绑定的值之一总是未定义:
项目列表模板
<div>
<table>
<thead>
<tr>
<th ng-repeat="column in itemContext.columns">{{column.heading}}</th>
</tr>
</thead>
<tbody>
<!-- Also experimented leaving item='item' out as well -->
<tr item="item" ng-repeat="item in itemList.items">
<item-column item="item" column="column" ng-repeat="column in itemContext.columns"></item-column>
</tr>
</tbody>
</table>
</div>
ItemColumn 模板
<!-- Problems here because item is undefined -->
<td ng-class="columnClasses(column, item)">{{column.itemField(item)}}</td>
指令
.directive('itemColumn', ["$rootScope", function($rootScope) {
return {
restrict: 'E',
replace: 'true',
templateUrl: '/templates/itemColumn.html',
scope:{
item: "=",
column: "="
},
link: function(scope, element, attrs) {
console.log("-- Loading item column --");
// sope.column is what I would expect, but scope.item is undefined
console.log(scope);
}
};
}])
关于我需要做什么才能从我的指令中的 ng-repeat 访问 item 绑定有什么想法吗?
一般来说,使用 ng-repeat 时是否需要创建冗余属性绑定:
<item-column item="item" column="column" ng-repeat="column in itemContext.columns"></item-column>
在我的指令中,我使用"="进行列绑定,所以我需要模板中的column="column"吗?
谢谢
【问题讨论】:
-
您期望一个名为
problem的属性,但调用者传递了一个名为item的属性。 -
抱歉,这是一个复制粘贴错误。应该是无处不在的项目。更新。谢谢。
标签: angularjs