如何检索从后端AngularJS常数 [英] How to retrieve constants for AngularJS from backend

查看:86
本文介绍了如何检索从后端AngularJS常数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想从后台检索,使他们通过注射提供给所有我的应用程序控制器的一些应用程序设置。什么是最角的方法呢?

i have some application settings that i want to retrieve from backend, so that they would be available to all of my application controllers via injection. What is the most angular-way to do that?

1)如果我只需要设置一个或几个控制器我可以通过路由解决方法来检索他们,但对全球的应用范围是什么?结果
2)我可以使用.RUN()方法,但由于通话将异步I都没有担保,在我访问控制器我的配置将被载入。

1) If i only needed settings for one or few controllers i could retrieve them via routing resolve method, but what about global application scope?
2) I could use the .run() method, but since call will be async i have no guaranties that my config will be loaded before i access controllers.

目前我的设置恢复为JSON对象,我的模板/ HTML文件只需通过Web服务器提供服务。所以我不能嵌入到任何脚本标记,解析HTML服务器端或任何类似的技术。

Currently my settings are returned as a json object, and my templates/html files are simply served by web server. So i cannot embed anything into script tags, parse html on the server side or any similar technique.

推荐答案

有一个整洁的插件来完成这项工作。 https://github.com/philippd/angular-deferred-bootstrap

There is a neat plugin to do the job. https://github.com/philippd/angular-deferred-bootstrap

您需要更改引导方法

deferredBootstrapper.bootstrap({
  element: document.body,
  module: 'MyApp',
  resolve: {
    APP_CONFIG: function ($http) {
      return $http.get('/api/demo-config');
    }
  }
});
angular.module('MyApp', [])
  .config(function (APP_CONFIG) {
    console.log(APP_CONFIG);
  });

这篇关于如何检索从后端AngularJS常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆