如何使用Angular JS常量值 [英] How to use angular js constants values
问题描述
我想删除角度控制器中的所有硬编码,为此,我想从另一个js文件中加载所有热编码,例如
I want to remove all hard codings in angular controller, For that I want to load all hot codings from another js file, For Example
modalHeader = "Success";
这里成功是一个热门编码.因此,我想从外部js文件加载此成功.在Js控制器中,我想提供成功的关键,而不是"Success"
,我引用了许多文章,但是这种情况没有用.请给出一些信息以继续进行,因为我比较熟悉angularJS
Here Success is a Hot coding. So I want to load this success from out side js file. In Js controller I want to give key of the success,instead of "Success"
, I reffered many articals But there is no useful for this scenario.Please give some info to proceed like this,as I am fresher to angularJS
谢谢.
推荐答案
您可以为此使用常量.常量值可以在整个应用程序中访问.您只需将常量注入控制器或服务中即可使用.
You can use constants for this . Constant values can be accessed all over the application . You just inject the constant into controller or service to use.
定义常量或值
var app = angular
.module('test');
app.constant("ENV", {
"CON1": "val1",
"CON2": "val2",
"CON3: "val3",
});
并用于服务
angular.module('test')
.service('testService', function ($http, ENV)
或在控制器中
angular.module('test')
.controller('testController ', function ($http, ENV)
值可通过ENV.CON1
这篇关于如何使用Angular JS常量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!