Angularjs the method of setting global variables Summary _ Basics

Source: Internet
Author: User

Angularjs three ways to set global variables

Angularjs has two of its own, set the global variable method, in addition to JS set the global variable method, a total of three kinds. The function to implement is that global variables defined in Ng-app can be used in different ng-controller.

1, the direct definition of global variable through VAR, this pure JS is the same.

2, use ANGULARJS value to set the global variable.

3, use ANGULARJS constant to set global variables.

Here is an example to illustrate the above 3 methods:

Instance:

1, in the app module, define global variables

' Use strict ';

/* APP Module *

/var test2 = ' Tank ';     Method 1, define global variable

var phonecatapp = angular.module (' Phonecatapp ', [   //define a Ng-app
 ' Ngroute ',
 ' Phonecatcontrollers ',
 ' tanktest '
]);

Phonecatapp.value (' test ', {"Test": "test222", "test1": "test111"}); Method 2 defines the global variable

phonecatapp.constant (' constanttest ', ' This is Constanttest ');  Method 3 defines the global variable

phonecatapp.config ([' $routeProvider ',        //Set routing
 function ($routeProvider) {
  $ Routeprovider.
   When ('/phones ', {
    templateurl: ' partials/phone-list.html '   //Here is not set controller, you can add Ng-controller to the module
   }).
   When ('/phones/:p Honeid ', {
    templateurl: ' partials/phone-detail.html ',
    controller: ' Phonedetailctrl '
   }).
   When ('/login ', {
    templateurl: ' partials/login.html ',
    controller: ' Loginctrl '
   }).
   Otherwise ({
    redirectto: '/login '
   });
 }];

2, calling global variables in controller

' Use strict ';

/* Controllers

/var phonecatcontrollers = angular.module (' Phonecatcontrollers ', []);

Phonecatcontrollers.controller (' Phonelistctrl ', [' $scope ', ' Test ', ' constanttest ',
 function ($scope, test, Constanttest) {
  $scope. test = test;          Method 2, the global variable is assigned to the $scope.test
  $scope. constanttest = constanttest;  Method 3, Assignment
  $scope. test2 = test2;         Method 1, Assignment
 }]);

3, look at the effect in HTML

<div data-ng-controller= "Phonelistctrl" >
  {{test.test1}
} {{constanttest}}} </div>

Result: test111 This is Constanttest tank

In fact, we can implement global variables in other ways, such as: Angularjs factory function.

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

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.