AngularJS:从ng重复动态分配控制器 [英] AngularJS: dynamically assign controller from ng-repeat
问题描述
< section ng-repeat =面板>
< divng-include ='path / to / file.html'ng-controller ={{panel}}>< / div>
< / section>
但是Angular抱怨说 {{panel}}
未定义。
我猜, {{panel}}
尚未定义(因为我可以在模板中回显 {{panel}}
。
已经看到很多人设置 ng-controller
等于像这样的变量的例子: ng-controller =template.ctrlr
。但是,没有创建一个重复的并发循环,我不知道如何在 ng-controller中使用
需要它。 {{panel}}
的值
PS我还尝试在我的模板中设置 ng-controller ={{panel}}
(认为它必须已经解决了),但没有骰子。
您的问题是ng控制器应该指向控制器本身,而不仅仅是控制器名称的字符串。
所以你可能想要将$ scope.sidepanels定义为数组,其中包含控制器的指针,可能是这样的:
$ scope.sidepanels = [警报,订阅];
这是js fiddle的工作示例 http://jsfiddle.net/ADukg/1559/
然而,我觉得这很奇怪您可能想要在ngRepeat中设置控制器的情况。
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>
But Angular complains that {{panel}}
is undefined.
I'm guessing that {{panel}}
isn't defined yet (because I can echo out {{panel}}
inside the template).
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. I also tried setting ng-controller="{{panel}}"
in my template (thinking it must have resolved by then), but no dice.
Your problem is that ng-controller should point to controller itself, not just string with controller's name.
So you might want to define $scope.sidepanels as array with pointers to controllers, something like this, maybe:
$scope.sidepanels = [Alerts, Subscriptions];
Here is the working example on js fiddle http://jsfiddle.net/ADukg/1559/
However, i find very weird all this situation when you might want to set up controllers in ngRepeat.
这篇关于AngularJS:从ng重复动态分配控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!