$rootScope 是 AngularJS app 中最上層的 scope,一個 app(ng-app) 只會有一個 $rootScope,也就是說同一個 app 可以共用同一個 $rootScope 的值
範例
module.js
var ballApp = angular.module('BallApp', ['ngRoute'], function($routeProvider) {
// Route
$routeProvider.when('/', {
templateUrl: 'view.html'
})
.when('/edit/:index', {
templateUrl: 'edit.html',
controller: 'EditCtrl'
})
.otherwise({
redirectTo: '/'
});
});
// Controller
ballApp.controller('NameCtrl', function($scope, $rootScope) {
$rootScope.persons = ['Johnson', 'Febr', 'Minnine'];
}).controller('EditCtrl', function($scope, $routeParams) {
$scope.index = $routeParams.index - 1;
// 因為 $scope 繼承 $rootScope 因此可以可以使用 persons
console.log($scope.persons);
});