刚到公司工作,也是刚接触AngularJS,在看代码的过程中经常碰到$rootScope和$scope有点晕。经过学习个人理解:
1.$Scope是AngularJS中controller中的数据和HTML页面沟通的中间域,也就是说HTML能获取$Scope里面的数据显示出来,如果你要让数据能在HMTL中使用,就需要把数据添加到$Scope中。例如$Scope.xx = 'xx'。
2.$rootScope则是所有controller进行数据沟通的中间域,即在$rootScope中的数据,在每个controller中都能通过$rootScope.xxx获取到。
phonecatApp.controller('Test',['$scope','$rootScope',
function($scope,$rootScope) {
$rootScope.name = 'word';
}
]);
phonecatApp.controller('Test2',['$scope','$rootScope',
function($scope,$rootScope) {
$scope.name = $rootScope.name;
}
]);