有没有在那里我可以在我的AngularJS应用程序定义一个全局$ scope.data变量的地方吗? [英] Is there a place where I can define a global $scope.data variable in my AngularJS app?
本文介绍了有没有在那里我可以在我的AngularJS应用程序定义一个全局$ scope.data变量的地方吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在AngularJS应用程序中使用 $ scope.data.xxxx
等许多地方。现在我宣布$ scope.data在这样每个控制器:
I use $scope.data.xxxx
etc in many places in my AngularJS application. Right now I am declaring $scope.data in each controller like this:
$scope.data = {}
有没有为我做这更好的地方。比如可以更好地我这样做在.config设置时,我的app.js文件或.RUN?
Is there a better place for me to do this. For example could I better do this when setting up my app.js file in the .config or .run ?
推荐答案
许多地方,你可以把它
var app= angular.module('myApp',[])
随着服务
app.factory('myData',function(){
return { foo:'bar'}
});
作为值
app.value( 'myData', { foo:'bar'});
由于恒
app.constant( 'myData', { foo:'bar'});
在每一种情况下将注入 myData的
在控制器指令,其他服务等,以访问
In each case would inject myData
in controller, directive, another service etc to access it
app.controller('SomeController',function($scope, myData){
$scope.data=myData;
})
这篇关于有没有在那里我可以在我的AngularJS应用程序定义一个全局$ scope.data变量的地方吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文