大佬教程收集整理的这篇文章主要介绍了AngularJS负载配置应用程序启动,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
注意:
– 我需要保存@L_607_0@参数在服务,所以我需要加载json文件内容前任何控制器加载,但服务单位可用
– 在我的情况下,使用外部json文件是必须的,因为应用程序客户端需要能够从外部文件轻松更新应用程序配置,而无需通过应用程序源。
听起来像你正在尝试做的是配置一个服务的参数。为了异步加载外部@L_607_0@,您必须在数据加载完成回调中自行引导角度应用程序,而不是使用自动加载。
对于实际上没有定义服务URL的服务定义(这将类似于contact-service.js)考虑这个例子:
angular.module('myApp').provider('contactsservice',function () { var options = { svcUrl: null,apiKey: null,}; this.config = function (opt) { angular.extend(options,opt); }; this.$get = ['$http',function ($http) { if(!options.svcUrl || !options.apiKey) { throw new Error('service URL and API Key must be configured.'); } function onContactsLoadComplete(data) { svc.contacts = data.contacts; svc.isAdmin = data.isAdmin || false; } var svc = { isAdmin: false,contacts: null,loadData: function () { return $http.get(options.svcUrl).success(onContactsLoadCompletE); } }; return svc; }]; });
然后,在文档准备好,您将调用加载您的@L_607_0@(在这种情况下,使用jQuery)。在回调中,您将使用加载的json数据执行angular应用程序.config。运行.config后,您将手动引导应用程序。非常重要:如果你使用这个方法,不要使用Ng-app指令,或者angular会引导它自己看到这个URL有更多的细节:
http://docs.angularjs.org/guide/bootstrap
像这样:
angular.element(document).ready(function () { $.get('/js/config/myconfig.json',function (data) { angular.module('myApp').config(['contactsserviceProvider',function (contactsserviceProvider) { contactsserviceProvider.config({ svcUrl: data.svcUrl,apiKey: data.apiKey }); }]); angular.bootstrap(document,['myApp']); }); });
以上是大佬教程为你收集整理的AngularJS负载配置应用程序启动全部内容,希望文章能够帮你解决AngularJS负载配置应用程序启动所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。