In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to set global variables in angular". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to set global variables in angular.
There are two functions for setting global variables in angualr, which are injected in the form of defining services, constant () and value (). Let's take a look at the code:
Please click on me 1 `value1` Please click on me 2 `value2` var app = angular.module ('myApp', []); app.constant (' myConstant', {"value1": "", "value2": "hello world", value3:1}) App.controller ('myCtrl1', function ($scope,myConstant) {$scope.onclick1 = function () {$scope.value1 = myConstant.value1 + (+ + myConstant.value3);};}); app.controller (' myCtrl2', function ($scope,myConstant) {$scope.onclick2 = function () {$scope.value2 = myConstant.value2 + (+ + myConstant.value3);} ); Please click on me 1 `value1` Please click on me 2 `value2` var app = angular.module ('myApp', []); app.value (' myConstant', {"value1": "", "value2": "hello world", value3:1}) App.controller ('myCtrl1', function ($scope,myConstant) {$scope.onclick1 = function () {$scope.value1 = myConstant.value1 + (+ + myConstant.value3);};}); app.controller (' myCtrl2', function ($scope,myConstant) {$scope.onclick2 = function () {$scope.value2 = myConstant.value2 + (+ + myConstant.value3);} })
The results of the above two codes are the same, and value3 will increase with each click, which once again proves the ability of value and constant to set global variables. Someone asked that the results of the two codes are the same, so what's the difference?
The difference between the two is that value cannot be injected into config, but constant can.
App.constant ('myConstant', {"value1": "", "value2": "hello world", value3:1}); app.value (' myValue', {"value1": "", "value2": "hello world", value3:1}); app.config (function (myValue) {.. });
Running the above code will report an error.
App.config (function (myConstant) {/ / you can get the 'myConstant'}' defined by constant); now that you have a better understanding of "how to set global variables in angular", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.