A summary of the difference between $scope and $rootscope in Anjularjs _angularjs

Source: Internet
Author: User

A word summary:

$rootScope is active for the global scope

$scope only takes effect for the current controller scope

Use the following example to prove the above statement:

Define a module named MyApp

var myApp = angular.module (' myApp ', []);

Create Onecontroller and Twocontroller these two controller

Onecontroller Incoming $scope and $rootscope

Myapp.controller (' Onecontroller ', [' $scope ', ' $rootScope ', function ($scope, $rootScope) {
 //local variables, $scope is displayed only in Onecontroller
 . One_language = ' Python ';

 Global variables, you can call
 $rootScope. Language = ' go ';
}]);

Twocontroller only incoming $scope

Myapp.controller (' Twocontroller ', [' $scope ', function ($scope) {
 //local variable, $scope is displayed only in Twocontroller
 . Two_ Language = ' Java ';
}];

HTML tag Content

<span ng-app= "myApp" >
  <style>
    div{margin-top:15px;border:2px solid rebeccapurple;width:400px;}
  </style>
  <div>
     
 

The results displayed

Summarize

The above is the full content of this article, please take a closer look at the above code, which helps you understand. If you have questions, you can leave a message and thank you for your support for the cloud-dwelling community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.