本文介绍了如何使用Angular Js声明全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个可以在所有控制器中使用的变量,但不想为其创建服务.
I want a variable that can be used in all the controllers but don't want to create a service for it .Is it possible in angular js?
推荐答案
您可以使用常量或值.
常量
var app = angular.module('myApp', []);
app.constant('appName', 'Application Name');
app.controller('TestCtrl', ['appName', function TestCtrl(appName) {
console.log(appName);
}]);
值
var app = angular.module('myApp', []);
app.value('usersOnline', 0);
app.controller('TestCtrl', ['usersOnline', function TestCtrl(usersOnline) {
console.log(usersOnline);
usersOnline = 15;
console.log(usersOnline);
}]);
http://ilikekillnerds .com/2014/11/constants-values-global-variables-in-angularjs-the-right-way/
这篇关于如何使用Angular Js声明全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!