如何设置角服务功能内$ rootScope? [英] How do you set $rootScope within Angular Service function?
问题描述
我无法设置$ rootScope为Angularjs。
下面是我的功能
App.controller('控制',
功能(UtilityService,$ rootScope){VAR setSession =功能(){ $ rootScope.test =是; //< - 我得到这个 UtilityService.getSession()。成功(
功能(){
$ rootScope.test =否; //< - 我没有得到这个我如何获得/设置这个值?
});
};setSession();});
其他信息:
一个可能的工作方式是建立被多个控制器之间的交互服务。是否有人知道如何与服务返回一个http.get JSON对象做到这一点。
我有麻烦在我的控制器动态范围,这是一个服务中被实例化。
为了解决我的问题,我不得不
1)通$ rootScope到我的第二控
App.controller($ rootScope){
块引用>2)设置我的第二个控制器的功能$ rootScope
$ rootScope.functionCall =功能(){};
块引用>3)设置我的传递价值$ rootScope($ rootScope.orderId)
$ rootScope.functionCall =功能(){
Service.getItems($ rootScope.orderId).success(
功能(结果){
$ scope.items =结果;
});
};
块引用>4)我的效用控制器,I环通我的结果,解析它们,将它们设置为$ rootScope你可以看到在#3中,我初始化$ rootScope.orderId
angular.forEach(结果,功能(值,键){
如果(关键!= NULL){
$解析(键).assign($ rootScope,价值);
}
});
块引用>5),我从我的服务调用内再呼叫控制器的功能!这是做什么神奇的,我把我的变量范围
$ rootScope.functionCall();
块引用>6)我也测试,以查看是否存在功能造成不同页面使用的公用code,但可能不具备的功能来执行
如果(typeof运算$ rootScope.functionCall =='功能')
块引用>VAR setSession =功能(){ UtilityService.getSession()。成功(
功能(结果){ //将范围的rootscope会议
angular.forEach(结果,功能(值,键){
如果(关键!= NULL){
$解析(键).assign($ rootScope,价值);
}
}); //不得不绕过范围的问题,因此必须执行这些FNS()
如果(typeof运算$ rootScope.functionCall =='功能'){
$ rootScope.functionCall();
}});};
setSession();I'm having trouble setting $rootScope for Angularjs.
Below is my function
App.controller('Controller', function (UtilityService, $rootScope) { var setSession = function () { $rootScope.test = "yes"; // <-- I get this UtilityService.getSession().success( function () { $rootScope.test = "No"; // <-- I don't get this How do I get/set this value? }); }; setSession(); });
Additional Info:
One of the ways that might work is to set up a service that is interacted between multiple controllers. Does anybody know how to do this with the service returning an http.get json object.
I'm having trouble getting a dynamic scope in my controller that is instantiated within a service.
解决方案In order to address my issue I had to
1) Pass $rootScope into my 2nd controller
App.controller($rootScope) {
2) Set my 2nd controller's function to $rootScope
$rootScope.functionCall = function () {};
3) Set my passed value to $rootScope ($rootScope.orderId)
$rootScope.functionCall = function () { Service.getItems($rootScope.orderId).success( function(results) { $scope.items = results; }); };
4) within my utility controller, I loop through my results, parsing them, and setting them to $rootScope as you can see in #3 I am initializing "$rootScope.orderId"
angular.forEach(results, function (value, key) { if (key != null) { $parse(key).assign($rootScope, value); } });
5) I am re-calling the controller's function from within my service call! This is what did the magic for me putting my variable "in scope"
$rootScope.functionCall();
6) I am also testing to see if the function exist cause different pages utilize the utility code but may not have the function to execute
if (typeof $rootScope.functionCall == 'function')
var setSession = function () { UtilityService.getSession().success( function (results) { // Place the rootscope sessions in scope angular.forEach(results, function (value, key) { if (key != null) { $parse(key).assign($rootScope, value); } }); // Have to bypass "scope" issues and thus have to execute these fns() if (typeof $rootScope.functionCall == 'function') { $rootScope.functionCall(); } }); }; setSession();
这篇关于如何设置角服务功能内$ rootScope?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!