javascript - how does angular search for variable through its $scope chain -
(function(){ var outer = 'foo'; (function(){ var inner = 'bar'; return [inner, outer]; })(); })(); the above code creates slow resolution inner function #outer need check nearest object first , see doesn't contain 'outer' , variable in next level of scope chain.
does following angular code behave exact same way? if should beware of scope creation , scope chain variable finding?
angular.module('app', [], function($rootscope){ $rootscope.rootvar = 'root variable'; }) .controller('ctrl1', function($scope){ $scope.var1 = rootvar; }) .controller('ctrl2', function($scope){ //nested $scope.var2 = rootvar; })
in angular you're not creating inner scopes in same way, you're passing variable contains "scope". can't reference rootvar, doesn't exist in current functions scope, except property on $scope passed in. need change references
angular.module('app', [], function($rootscope){ $rootscope.rootvar = 'root variable'; }) .controller('ctrl1', function($scope){ $scope.var1 = $scope.rootvar; }) .controller('ctrl2', function($scope){ //nested $scope.var2 = $scope.rootvar; })
Comments
Post a Comment