Angular两个不同的指令具有相同的控制器共享范围

时间:2022-06-11 08:18:35

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.

我正在创建两个使用相同控制器的指令,因此我看到两个指令在控制器之间共享数据。我想要的是每个指令的数据都是UNIQUE,所以不应该共享数据。

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

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.

这个例子是一个非常复杂的应用程序的一小部分,所以我必须能够使用这种方法而不是其他方法。

2 个解决方案

#1


5  

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.

有关定义如何为指令初始化范围的更多信息,请查看此文章。

#2


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 .

正如您在文档中提到的那样,它们都共享相同的范围对象,无论它们是不同的实例,还是ng-modal指向同一个对象。这是双向绑定的角度的默认性质。

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

每当scope对象中的属性更新时。引用的ng-modal已更新

#1


5  

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.

有关定义如何为指令初始化范围的更多信息,请查看此文章。

#2


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 .

正如您在文档中提到的那样,它们都共享相同的范围对象,无论它们是不同的实例,还是ng-modal指向同一个对象。这是双向绑定的角度的默认性质。

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

每当scope对象中的属性更新时。引用的ng-modal已更新