0

I'm creating two directives which use the same controller, as a result I see that both directives share data among controller. What I want is that data to be UNIQUE per directive, so data shouldn't be shared.

var app = angular.module("app",[]);

app.controller('myCtrl', function($scope){
  $scope.data = {
    name: 'Javier'
  };
});


app.directive('dir1', function(){
  return {
    template: "<div style='background:red'><input type='text' ng-model='data.name'>{{data.name}}</div>",
    controller: "myCtrl"
  };
});


app.directive('dir2', function(){
  return {
    template: "<div style='background:yellow'><input type='text' ng-model='data.name'>{{data.name}}</div>",
    controller: "myCtrl"
  };
});

https://jsbin.com/vetikuvada/1/edit?html,js,output

So in my example I want is when I edit the text in one of the textboxes it doesn't trigger changes in the other textbox. I know that controllers are deployed with different instances but somehow they share the scope, I need this to be completely different. Is this possible?

The example is a small part of a very complex app, so I must be able to do it using this approach and no other else.

Manjar
  • 3,159
  • 32
  • 44

2 Answers2

6
app.directive('dir1', function(){
  return {
    /* defines an isolated scope where state is
       not shared between other scopes */
    scope: {}, 

    template: "<div style='background:red'><input type='text' ng-model='data.name'>{{data.name}}</div>",
    controller: "myCtrl"
  };
});

Review this post for more information on defining how scopes can be initialized for directives.

Community
  • 1
  • 1
Alex
  • 34,899
  • 5
  • 77
  • 90
0

As you mentioned in the document they both share same scope object , no matter they are different instances or not but ng-modal point to same object . This is the default nature of angular the two way binding .

whenever the property in the scope object got updated . The ng-modal that refers got updated

Vignesh
  • 496
  • 1
  • 4
  • 13