[英]Change ng-include template rendered inside ng-repeat
I've got an ng-repeat where each of the instances contains an ng-include
. 我有一个ng-repeat,其中每个实例都包含一个
ng-include
。 Like so: 像这样:
<li ng-repeat="item in items">
<div class="item-actions" ng-include="actionsTemplate"></div>
</li>
I would now like to be able to set and remove the actionsTemplate
source with the click of a button in the same scope, like so: 现在,我希望能够通过单击同一作用域中的按钮来设置和删除
actionsTemplate
源,如下所示:
<li ng-repeat="item in items">
<div class="item-actions" ng-include="actionsTemplate"></div>
<button ng-click="toggleActionsTemplate()">Toggle Actions</button>
</li>
So clicking the button would trigger a function toggleActionsTemplate()
to set the actionsTemplate
source to foo
, or if it's already set to foo
, set it to null
to remove the template. 所以点击按钮会触发一个功能
toggleActionsTemplate()
所设置的actionsTemplate
源foo
,或者如果它已经设置为foo
,将其设置为null
以除去模板。
I've tried doing this but can't figure out how get a controller function to target the template inside the same ng-repeat
instance the function is being called from. 我已经尝试过这样做,但无法弄清楚如何获得一个控制器函数来定位从其调用该函数的同一
ng-repeat
实例内的模板。
Is this possible, and more importantly, is this a good way of going about achieving this behaviour? 这是否可能,更重要的是,这是实现这一行为的好方法吗?
Here is a plunker: http://plnkr.co/edit/4p56QeuqpMAH1yc0tbfy?p=preview 这是一个小矮人: http ://plnkr.co/edit/4p56QeuqpMAH1yc0tbfy?p=preview
actionsTemplate
variable for each child scope inside ngRepeat
. ngRepeat
每个子作用域初始化一个不同的actionsTemplate
变量。 ngInit
to initialize a scope variable. ngInit
初始化作用域变量。 <li ng-repeat="item in items" ng-init="action = actionsTemplate">
<div class="item-actions" ng-include="action"></div>
<button ng-click="action = toggleAction(action)">Toggle Actions</button>
</li>
controller: 控制器:
$scope.toggleAction= function(action){
return action === 'foo' ? null : 'foo';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.