Difference between $scope and $rootScope

后端 未结 9 1764
孤城傲影
孤城傲影 2020-11-28 02:33

Can anyone explain the difference between $scope and $rootScope?

I think

$scope:

We can get ng-model properties in particular cont

9条回答
  •  一生所求
    2020-11-28 02:48

    The main difference is the availability of the property assigned with the object. A property assigned with $scope cannot be used outside the controller in which it is defined whereas a property assigned with $rootScope can be used anywhere.

    Example: If in the example below you replace $rootScope with $scope the department property will not be populated from the first controller in the second one

    angular.module('example', [])
      .controller('GreetController', ['$scope', '$rootScope',
        function($scope, $rootScope) {
          $scope.name = 'World';
          $rootScope.department = 'Angular';
        }
      ])
      .controller('ListController', ['$scope',
        function($scope) {
          $scope.names = ['Igor', 'Misko', 'Vojta'];
        }
      ]);
    
    
    
      
    Hello {{name}}!
    1. {{name}} from {{department}}

提交回复
热议问题