0

I have a controller:

MyController
- $scope.value = 5
- $scope.list = [
  {item: Apple, cost: 5},
  {item: Bannana, cost: 2}
]

In my index.html I have an ng-repeat on:

<div ng-controller="MyController">
                        <mydirective ng-repeat="item in list" value="value"></mydirective>
                    </div> 

In my directive, I have an isolate scope:

{
value:"=" 
}

In the linking function of the directive I have an bind to an event:

- Onclick, increment scope.value by "1"

The issue is it appears that despite the "=", each of the 2 directives generated from ng-repeat are merely "copies" of the actual MyController "value" variable. How do I make it such that they are linked together so that when I click, the $scope.value of myController gets updated and all the directive "scope.value" would match it? Or is this not possible?

I want to be able to watch on $scope.value of MyController from all the directives so that each of the directives can do something based on "value".

Setsuna
  • 2,081
  • 7
  • 32
  • 50

1 Answers1

1

I suppose you hit the problem of your scopes inheriting primitive types. Try using an object instead:

MyController
- $scope.valueObject = { value: 5 }
then increment valueObject.value by "1"

Each scope of your ng-repeat inherits from the controllers scope. If you inherit a primitive type like your "value", then changing it in the child scope will only affect this child as it shadows the parent. If you inherit an object instead, then changing the value would affect the inherited object = the value would be the same for all ng-repeat scopes.

Community
  • 1
  • 1
PeterFromCologne
  • 10,213
  • 9
  • 36
  • 46
  • How would a watch statement be formed for the directive to monitor that "value"? It seems scope.$watch('valueObject.value',...) does not work. I want to make it such that when that value changes, all directives "get it" and do something. – Setsuna Mar 23 '14 at 20:46
  • Maybe you could make a plunker to elaborate further on your problem? http://plnkr.co/ there are Angular JS templates that make it easy to create a running example. – PeterFromCologne Mar 23 '14 at 21:11