如何使用定模块中的角? [英] How to use constant module in angular?

查看:77
本文介绍了如何使用定模块中的角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的角度应用程序,我有一个设置模块,我做了一个常数。我想,它应该是一个恒定的,因为得到应用的常量先于其他提供方法

In my angular application I have a settings module that I made a constant. I figured that it should be a constant because the constants get applied before other provide methods.

我也有2服务: getUserIdService getTokenService 这让我用户id code>和标记。

I also have 2 services: getUserIdService and getTokenService that get me userId and token respectively.

angular.module('app').constant('settings', {
    streamServer: 'http://someurl.com:9009',
    userId: /* $getUserIdService.getUserId() */
    token: /* $getTokenService.getToken() */
});

从我的API看到没有一个构造函数,所以我不能在任何依赖传( getUserIdService getTokenService

From what I see in the api constant doesn't have a constructor so I can't pass in any dependencies (getUserIdService, getTokenService)

此模块需要全局可用,它不必须是恒定的。我只是需要任何其他模块之前它来获得初始化。

This module needs to be globally available and it doesn't have to be a constant. I just need it to get initialized before any other module.

我怎样才能做到这一点?

How can I do this?

推荐答案

使用角模块运行的方法来初始化数据。你可以注入你的服务变成run方法,并用它来初始化数据

Use the angular module run method to initialize your data. You can inject your services into the run method and use it to initialize your data

样品演示: http://plnkr.co/edit/TW5r7YM5gkevWr2hjmZs?p= preVIEW

JS:

var app = angular.module('plunker', []);

app.value('settings', {
    streamServer: 'http://someurl.com:9009',
    userId:null ,/* $getUserIdService.getUserId() */
    token: null/* $getTokenService.getToken() */
}).run(function($http,$timeout,settings){//inject your services here
  settings.userId = 'guru';//call services to get the user id
  settings.token = 'tkn39';
});

这篇关于如何使用定模块中的角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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