Angularjs-从module.config调用函数
我有一个密码Angularjs-从module.config调用函数,angularjs,config,angularjs-scope,restangular,Angularjs,Config,Angularjs Scope,Restangular,我有一个密码 var viewerAngular = angular.module('ngAppDemo', ['restangular','btford.socket-io']) .config(function(RestangularProvider) { $.get('../config/config.xml', function(data) { $(data).find('contentserver').each(function() {
var viewerAngular = angular.module('ngAppDemo', ['restangular','btford.socket-io'])
.config(function(RestangularProvider) {
$.get('../config/config.xml',
function(data) {
$(data).find('contentserver').each(function() {
serverDetails.contentserver = assignServerDetails($(this));
var restprovider = RestangularProvider;
restprovider.setBaseUrl("http://"+serverDetails.contentserver.ip+":"+serverDetails.contentserver.port+"\:"+serverDetails.contentserver.port);
//$scope.init();
});
});
我需要在读取config../config/config.xml文件后调用函数init。 我得到一个错误ReferenceError:$scope未定义
如何在module.config中添加$scope?或者如何从配置中调用函数?如果您需要在配置中的每个作用域中添加一些内容,您可以使用$rootScope,但更好的做法是为该数据创建一个服务。例如,在配置阶段,您不能请求—您只能请求提供程序 var-app=angular.module'app',[]; //配置内容 app.configfunction$routeProvider$locationProvider{ //您可以在此处插入任何提供程序 }; //运行块 app.runfunction$rootScope{ //您可以在这里注入任何实例 };
有关更多信息,请参阅。不要这样做。仅仅因为你能做一些事情,并不意味着这是一件好事。我建议你重新组织你的应用程序。@baba我没听懂。我该怎么办?在您的配置中,不要访问作用域,也不要对其调用任何函数。无论您在做什么,都有其他方法可以做到这一点。我需要在读取config../config/config.xml文件后调用函数init。通常,配置是在呈现控制器之前完成的,因此您不必从配置中调用init,但可以立即从控制器中调用它。我必须在控制器内使用serverDetails.contentserver。那我怎么办?
var viewerAngular = angular.module('ngAppDemo', ['restangular','btford.socket-io'])
.config(function(RestangularProvider) {
$.get('../config/config.xml',
function(data) {
$(data).find('contentserver').each(function() {
serverDetails.contentserver = assignServerDetails($(this));
var restprovider = RestangularProvider;
restprovider.setBaseUrl("http://"+serverDetails.contentserver.ip+":"+serverDetails.contentserver.port+"\:"+serverDetails.contentserver.port);
var Scope=angular.element(document.querySelector('[ng-controller="controllerName"]')).scope();
Scope.init();
});
});