EmptyData
EmptyData

Reputation: 2576

How to use directive inside ng-repeat?

I want to use a counter http://plnkr.co/edit/eRWPuEP8LdQBbHksIL3S?p=info directive inside ng-repeat like

<div ng-repeat="item in items">
    <div counter value="item" min="1" max="30" step="3" editable></div>
</div>

in my controller

$scope.items = []
items.push(1);
items.push(2);

But it is not working.

Edit:1

in my controller

$scope.items = []
$scope.items.push(1);
$scope.items.push(2);

Edit 2:

My Plunkr : http://plnkr.co/edit/ji2ppBp0PyRCzAT4UhMj?p=preview

Upvotes: 0

Views: 126

Answers (1)

hjl
hjl

Reputation: 2802

ng-repeat creates a child scope, item is passed to the child scope as a primitive type. even you isolates the scope in your directive with 2-way binding for value, the value will not be changed, ie. scope.items is always [1, 2]

solution is to pass an object (reference) to child scope, to achieve this:

$scope.items = []
$scope.items.push({val:1});
$scope.items.push({val:2});

// template of counter directive
<input type="text" class="counter-field" ng-model="value.val">

var setValue = function( val ) {
    scope.value.val = parseInt( val );
};

you might also need to change minus and plus methods to refer to scope.value.val

Upvotes: 1

Related Questions