Angularjs 3 ways to set global variables

Source: Internet
Author: User
Tags vars

There are two kinds of Angularjs itself, the method of setting global variables, and the method of setting global variables with js, there are altogether three KINDS. The function to be implemented is that the global variables defined in the Ng-app can be used in different ng-controller.

1, the direct definition of global variable by var, This pure JS is the Same.

2, set the global variable with ANGULARJS Value.

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

View Copy print?
  1. ' Use strict ';
  2. /* App Module */
  3. var test2 = ' Tank '; //method 1, Define global variables
  4. var Phonecatapp = angular.module (' Phonecatapp ', [ //define a Ng-app
  5. ' Ngroute ',
  6. ' Phonecatcontrollers ',
  7. ' Tanktest '
  8. ]);
  9. Phonecatapp.value (' test ', {"test":"test222","test1":"test111"}); //method 2 Define global variables
  10. Phonecatapp.constant (' constanttest ', ' This is constanttest '); //method 3 Define global variables
  11. Phonecatapp.config ([' $routeProvider ', //set Route
  12. function ($routeProvider) {
  13. $routeProvider.
  14. When ('/phones ', {
  15. Templateurl: ' partials/phone-list.html ' //the controller is not set here and can be added to the module Ng-controller
  16. }).
  17. When ('/phones/:p honeid ', {
  18. Templateurl: ' partials/phone-detail.html ',
  19. Controller: ' Phonedetailctrl '
  20. }).
  21. When ('/login ', {
  22. Templateurl: ' partials/login.html ',
  23. Controller: ' Loginctrl '
  24. }).
  25. otherwise ({
  26. Redirectto: '/login '
  27. });
  28. }]);

2, calling the global variable in the controller

View Copy print?
  1. ' Use strict ';
  2. /* Controllers */
  3. var phonecatcontrollers = angular.module (' phonecatcontrollers ', []);
  4. Phonecatcontrollers.controller (' Phonelistctrl ', [' $scope ',' Test ', 'constanttest ',
  5. function ($scope, test,constanttest) {
  6. $scope. test = test; //method 2, Assign the global variable to $scope.test
  7. $scope. constanttest = constanttest; //method 3, Assignment
  8. $scope. test2 = test2; //method 1, Assignment
  9. }]);

3, look at the effect in HTML

View Copy print?
    1. <div data-ng-controller="phonelistctrl" >
    2. {{test.test1}}
    3. {{constanttest}}
    4. {{test2}}
    5. </div>
    6. Result: test111 This is Constanttest tank

In fact, we can use other methods to implement global variables, such as: Angularjs factory Function.

83

Address: http://blog.51yip.com/jsjquery/1601.html

Angularjs 3 ways to set global variables

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.