角传球范围NG-包括 [英] Angular passing scope to ng-include
问题描述
我有一个控制器,我写的,我在我的应用程序的多个地方使用带有 NG-包括
和 NG-重复
,就像这样:
< DIV
NG-重复=项中的项目
NG-包括='item.html'
NG-控制器=ItemController
>< / DIV>
在控制器/模板,我期待项目
的价值存在,整个事情是围绕这一理念构建的。但现在,我需要在一个稍微不同的方式使用控制器,而不 NG-重复
,但仍需要能够在一个项目
。我看到 NG-INIT
,认为它可以做我所需要的,是这样的:
< DIV
NG-的init =项= leftItem
NG-包括='item.html'
NG-控制器=ItemController
>< / DIV>
< DIV
NG-的init =项= rightItem
NG-包括='item.html'
NG-控制器=ItemController
>< / DIV>
但是,这似乎并不奏效。任何人有任何想法如何,我可以在一个变量传递的范围,这样一个奇异的实例?
编辑:
高于此控制器加载在 leftItem
和 rightItem
的价值观,是这样的:
.controller('MainController',函数($范围,ItemModel){
ItemModel.loadItems()
。然后(功能(项){
$范围。$应用(函数(){
$ scope.leftItem = items.left;
$ scope.rightItem = items.right;
});
});
});
我最终改写成一个指令,以
结合的范围内所需要的值 范围:{
项:'='
}
I have a controller that I wrote that I use in multiple places in my app with ng-include
and ng-repeat
, like this:
<div
ng-repeat="item in items"
ng-include="'item.html'"
ng-controller="ItemController"
></div>
In the controller/template, I expect the item
value to exist, and the whole thing is built around this idea. Now, though, I need to use the controller in a slightly different way, without the ng-repeat
, but still need to be able to pass in an item
. I saw ng-init
and thought it could do what I needed, like this:
<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>
But that does not seem to be working. Anyone have any ideas how I can pass in a variable for scope in a singular instance like this?
Edit:
The controller above this is loading in the leftItem
and rightItem
values, something like this:
.controller('MainController', function($scope, ItemModel) {
ItemModel.loadItems()
.then(function(items) {
$scope.$apply(function() {
$scope.leftItem = items.left;
$scope.rightItem = items.right;
});
});
});
I ended up rewriting it into a directive and binding the needed value in the scope with
scope: {
item: '='
}
这篇关于角传球范围NG-包括的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!