我有一个控制器,我在我的应用程序中多次使用 ng-include
和 ng-repeat
。如下:
<div
ng-repeat="item in items"
ng-include="'item.html'"
ng-controller="ItemController"
></div>
在控制器/模板中,我希望item
的值存在,并且整个内容都是基于这个想法构建的。现在,我需要以稍微不同的方式使用控制器,而不使用ng-repeat
,但仍然需要能够传入一个item
。我看到了ng-init
,认为它可以实现我所需的功能,就像这样:<div
ng-init="item = leftItem"
ng-include="'item.html'"
ng-controller="ItemController"
></div>
<div
ng-init="item = rightItem"
ng-include="'item.html'"
ng-controller="ItemController"
></div>
但是似乎这并没有起作用。有人有想法怎样才能在这种特定情况下传递一个作用域变量吗?编辑: 在上面的控制器中,会加载左侧项和右侧项等值,类似于以下内容:
.controller('MainController', function($scope, ItemModel) {
ItemModel.loadItems()
.then(function(items) {
$scope.$apply(function() {
$scope.leftItem = items.left;
$scope.rightItem = items.right;
});
});
});