【发布时间】:2013-05-06 08:04:19
【问题描述】:
如果您希望在工作代码中查看问题,请从这里开始:http://jsbin.com/ayigub/2/edit
考虑用这种几乎等效的方式来编写一个简单的指令:
app.directive("drinkShortcut", function() {
return {
scope: { flavor: '@'},
template: '<div>{{flavor}}</div>'
};
});
app.directive("drinkLonghand", function() {
return {
scope: {},
template: '<div>{{flavor}}</div>',
link: function(scope, element, attrs) {
scope.flavor = attrs.flavor;
}
};
});
当单独使用时,这两个指令的工作方式和行为相同:
<!-- This works -->
<div drink-shortcut flavor="blueberry"></div>
<hr/>
<!-- This works -->
<div drink-longhand flavor="strawberry"></div>
<hr/>
但是,当在 ng-repeat 中使用时,只有快捷方式版本有效:
<!-- Using the shortcut inside a repeat also works -->
<div ng-repeat="flav in ['cherry', 'grape']">
<div drink-shortcut flavor="{{flav}}"></div>
</div>
<hr/>
<!-- HOWEVER: using the longhand inside a repeat DOESN'T WORK -->
<div ng-repeat="flav in ['cherry', 'grape']">
<div drink-longhand flavor="{{flav}}"></div>
</div>
我的问题是:
- 为什么手写版本在 ng-repeat 中不起作用?
- 如何让手写版本在 ng-repeat 中工作?
【问题讨论】:
标签: javascript angularjs angularjs-directive