Angular.js learns the Run method of the two---$scope and $rootscope,angular modules, and the code compression in the dependency injection

Source: Internet
Author: User

I. The difference between $scope and $rootscope

A sentence summary:

$rootScope the scope for the global takes effect

$scope only valid for the current controller scope

Second, the Angularjs module of the Run method

The Run method initializes the global data and only works on the global scope, such as $rootscope


var m1 = Angular.module (' myApp ', []); M1.run ([function ($rootScope) { = ' Hello '; }]); </script>

Third, the code compression problem in Dependency injection

Define a controller, usually by the following code, but in the process of code compression, the parameters in the function may change, $scope may become $sc, or other (here the change is not controllable), once changed, the following bound value will be wrong.

var app = Angular.module ("myApp", []); App.controller (function  ($scope) {  = ' Zhang San ';});

To solve this problem, add [] outside the function and pass in the string as shown in the following code, because the string does not change during compression .

var app = Angular.module ("myApp", []); App.controller (function  ($scope) {  = ') Zhang San ';}]);

Angular.js learns the Run method of the two---$scope and $rootscope,angular modules, and the code compression in the dependency injection

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.