English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In a word:
$rootScope takes effect within the global scope
$scope only takes effect within the current controller scope
The following example is used to prove the above statement:
Define a module named myApp
var myApp = angular.module('myApp', []);
Create two controllers, oneController and twoController
oneController passes $scope and $rootScope
myApp.controller('oneController', ['$scope', '$rootScope', function ($scope, $rootScope) { // Local variables, only visible in oneController $scope.one_language = 'Python'; // Global variables, can be called $rootScope.language = 'Go'; });
twoController passa solo $scope
myApp.controller('twoController', ['$scope', function ($scope) { // Variabile locale, visibile solo nel twoController $scope.two_language = 'Java'; });
Contenuto dell'etichetta HTML
<span ng-app="myApp"> <style> div{margin-top: 15px;border: 2px solid rebeccapurple;width: 400px;} </style> <div> <h3>Sono la variabile globale language: {{ language}}</h3> </div> <div ng-controller="oneController"> <h3>Sono la variabile locale one_language: {{ one_language}}</h3> </div> <div ng-controller="twoController"> <h1>twoController</h1> <h3>Sono la variabile locale two_language: {{ two_language }}</h3> <h3>Sono la variabile locale one_language: {{ one_language}}</h3> <h3>Sono la variabile globale language: {{ language }}</h3> </div> </span>
Risultato visualizzato
Sommario
Questo è tutto il contenuto dell'articolo, si prega di leggere attentamente il codice sopra, questo può aiutarti a comprendere meglio. Se hai domande, puoi lasciare un commento per discuterle, grazie per il supporto al tutorial di urlo.