AngularJS:动态分配从纳克重复控制器 [英] AngularJS: dynamically assign controller from ng-repeat
问题描述
我想动态分配一个控制器包含的模板,像这样:
I'm trying to dynamically assign a controller for included template like so:
<section ng-repeat="panel in panels">
<div ng-include="'path/to/file.html'" ng-controller="{{panel}}"></div>
</section>
但角抱怨说 {{}面板}
是不确定的。
我猜 {{}面板}
没有定义的没有的(因为我可以附和了 {{}面板}
在模板中)。
I'm guessing that {{panel}}
isn't defined yet (because I can echo out {{panel}}
inside the template).
我见过很多人设置 NG-控制器
等于像这样一个变量的例子: NG-控制器=template.ctrlr
。但是,如果没有创建重复concurrant循环,我想不出如何让 {{}面板}
时可用 NG-控制器的价值
需要它。
I've seen plenty of examples of people setting ng-controller
equal to a variable like so: ng-controller="template.ctrlr"
. But, without creating a duplicate concurrant loop, I can't figure out how to have the value of {{panel}}
available when ng-controller
needs it.
P.S。我也试着设置 NG-控制器={{面板}}
在我的模板(认为它必须那么已经解决),但没有骰子。
P.S. I also tried setting ng-controller="{{panel}}"
in my template (thinking it must have resolved by then), but no dice.
推荐答案
您的问题是,NG-控制器应指向控制器本身,没有与控制器的名字只是字符串。
Your problem is that ng-controller should point to controller itself, not just string with controller's name.
所以,你可能要定义$ scope.sidepanels作为数组指针控制器,这样的事情,也许是:
So you might want to define $scope.sidepanels as array with pointers to controllers, something like this, maybe:
$scope.sidepanels = [Alerts, Subscriptions];
下面是JS小提琴 http://jsfiddle.net/ADukg/1559/
不过,我觉得很奇怪这一切的情况下,您可能希望设置控制器将在ngRepeat。
However, i find very weird all this situation when you might want to set up controllers in ngRepeat.
这篇关于AngularJS:动态分配从纳克重复控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!