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: '='
}