简体   繁体   English

了解angularJS中的控制器范围

[英]understanding the controller scope in angularJS

Consider two div areas as follows, in html file 在html文件中考虑以下两个div区域

<div class="divArea1" ng-controller="myController">
   <input ng-click="updateName()" type="button" value="button"/>
</div>

<div class="divArea1" ng-controller="myController">
  <p>{{name}}</p>
</div>

Following is the angular js example 以下是角度js示例

productApp.controller("myController", [ '$scope', function($scope) {
    $scope.name= "XYZ";
    $scope.updateName= function() {
        $scope.name = "ABC";
    };
} ]);

problem is when I am trying to update the name, upon click on update button it is not visible in the second in the div area. 问题是当我尝试更新名称时,单击更新按钮后,它在div区域的第二个位置不可见。 Is there any mistake i am doing. 我在做任何错误吗?

What you have is two different controllers (with two separate scopes) with the same name. 您拥有的是两个具有相同名称的不同控制器(具有两个单独的作用域)。

You need to put the controller in the parent controller to keep the name in the same scope as the button: 您需要将控制器放在父控制器中,以使名称与按钮的作用域相同:

<div id="container" ng-controller="myController">
   <div class="divArea1">
      <input ng-click="updateName()" type="button" value="button"/>
   </div>

   <div class="divArea1">
     <p>{{name}}</p>
   </div>
</div>

here is demo http://jsfiddle.net/wg7pb1yu/3/ inject $rootScope so that it will do from global scope 这是演示http://jsfiddle.net/wg7pb1yu/3/注入$ rootScope,以便它将在全局范围内执行

productApp.controller("myController", [ '$scope','$rootScope', function($scope, $rootScope) {
$rootScope.name= "XYZ";
$scope.updateName= function() {
    $rootScope.name = "ABC";

};} ]);

Hope this will help you 希望这个能对您有所帮助

Controllers are not singletons. 控制器不是单例。 Each time you have a new controller, you're having a new instance of this controller, with a new isolated scope. 每次您有一个新的控制器时,您都将拥有该控制器的新实例以及新的隔离范围。

If your need is to share data between controllers, you should use a factory (which is a singleton). 如果需要在控制器之间共享数据,则应使用工厂(单例)。

angular.module('app').factory('mySharedData', function(){
   var service = {
       object : objectToShare
   };

   var objectToShare =  {};

   return service;
});

And from your controller : 从您的控制器:

angular.module('app').controller('myController', 
    ['$scope','mySharedData', 
    function($scope, mySharedData){
        $scope.someObject = mySharedData.object;
        $scope.updateName= function() {
            $scope.someObject.name = "ABC";
        };
    }
]);

And from your view : 从您的角度来看:

<div class="divArea1" ng-controller="myController">
   <input ng-click="updateName()" type="button" value="button"/>
</div>

<div class="divArea1" ng-controller="myController">
  <p>{{someObject.name}}</p>
</div>

Note : I've encapsulated the name property into an object because objects are passed by reference, and strings by value. 注意:我将name属性封装到一个对象中,因为对象是通过引用传递的,而字符串是通过值传递的。 This allows you to make it easier to share your values and to have it automatically updated into the service and other controllers, without having to access your property through accessors. 这使您可以更轻松地共享您的值并将其自动更新到服务和其他控制器中,而不必通过访问器访问属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM