1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > angular中如何定义全局变量_在angular中定义模块的全局变量有哪些方法?示例代码?...

angular中如何定义全局变量_在angular中定义模块的全局变量有哪些方法?示例代码?...

时间:2019-08-11 10:45:00

相关推荐

angular中如何定义全局变量_在angular中定义模块的全局变量有哪些方法?示例代码?...

1.一般来说,是不建议在$rootScope上面绑定过多的变量,这样一来程序的可维护性就会变差;当然只是不建议,特殊情况特殊处理;比如网站的title标题可能要经常换,所以这个绑定在$rootScope还是个不错的选择。

2.Angular提供了两种方法,一种方法就是你说的那个,还有就是下面的:

(function() {

'use strict';

angular

.module('app')

.constant('toastr', toastr)

.constant('moment', moment);

})();

3.一般来说使用value和constant已经可以了。

1.我看你的情况应该是想在整个应用中使用这个函数,那么你可以写在服务中啊,Angular的Service就是为了提供全局公用的方法;上面的使用方法是为了可以使用一些外部的插件,或者配置一些应用的信息而使用的,我这边写了一个例子,你可以看看,传送门。

2.具体的代码可以看下面:

引入文件的顺序

index.html

constant

{{vm.test}}

{{vm.my_key}}

module.js

(function(window){

// ..

// exports

var Test = {

hello: function(){

console.log('hello');

}

};

window.Test = Test;

})(window);

app.js

(function(){

angular.module('MyApp', [])

.constant('Test', Test)

.constant('MyKey', 'q123nasbd12y38basd237y')

.controller('MyController', MyController)

.service('Service', Service);

MyController.$inject = ['Test', 'Service', 'MyKey'];

Service.$inject = [];

function Service(){

var service = {

info: info

};

return service;

function info(){

return 'info';

}

}

function MyController(Test, Service, MyKey){

var vm = this;

vm.test = Service.info();

vm.my_key = MyKey;

Test.hello();

}

})();

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。