undefined
undefined

Reputation: 3632

Shared scope variable is not updating across directive

I am having a strange issue with directive, I am trying to create the plunkr, but before that posting this to SO, to see if I get any response.

I have a following variable in controller:

$scope.sharedVar = [];
$scope.loopIs = ["var1", "var2", "var3"];

Now I am passing this variable to direective through ng-repeat:

<div class="" ng-repeat="val in loopIs">
  <my-directive val="val" sharedVar="sharedVar"></my-directive>
</div>

And Here is my directive:

app.directive(myDirective){
 return {
  restrict: 'E',
  scope: {
    val: '=val',
    sharedVar: '=sharedVar'
   },
   templateUrl: 'template.html',
   link: function(scope){
      //...........My logic here
   }
  }
}

sharedVar is shared across directives, so I want this to get updated if it changes from any directive.

Can any one please help me here. Am I doing anything wrong with directive inside ng-repeat?

========update========

i have added the plunkr.. please have a look.

https://plnkr.co/edit/lhE3UwjJzqNgk2jd8BIV?p=preview

Upvotes: 1

Views: 35

Answers (1)

xkeshav
xkeshav

Reputation: 54022

use $parent as ng-repeat create separate scope itself

 <my-directive val="val" shared-var="$parent.sharedVar"></my-directive>

Working Demo

Upvotes: 1

Related Questions