[英]Angular.js: Is .value() the proper way to set app wide constant and how to retrieve it in a controller
Hi there I was watching a couple of the angular.js videos and saw that the value() method was used to set a kind of module-wide constant. 您好,我正在观看几个angular.js视频,并看到value()方法用于设置一种模块范围的常量。 for example, one can set the Angular-UI library's config like so: (coffeescript) 例如,可以像这样设置Angular-UI库的配置:(coffeescript)
angular.module('app',[])
.value "ui.config",
tinymce:
theme: 'simple'
width: '500'
height: '300'
And my app is currently looking like this: 我的应用程序目前看起来像这样:
window.app = angular.module("app", [ 'ui'])
.config(["$routeProvider", ($routeProvider) ->
$routeProvider
.when "/users",
templateUrl: "assets/templates/users/index.html"
controller: IndexUsersCtrl
.otherwise redirectTo: "/users"
])
.value 'csrf', $('meta[name="csrf-token"]').attr('content') #<---- attention here
IndexUsersCtrl = ($scope) ->
$scope.users = gon.rabl
console.log "I want to log the csrf value here" #<---- then attention
IndexUsersCtrl.$inject = ['$scope']
But I can't seem to get that value by tapping into the 'app' variable which is corresponding to the app module. 但是我似乎无法通过点击与app模块相对应的'app'变量来获得该值。
I read up here on ST and over on angularjs's google group that one way to share common code btwn controllers is through a service, will this concept apply here, too? 我在ST上阅读了这篇关于angularjs谷歌小组的消息,通过服务分享公共代码btwn控制器的一种方法是,这个概念也适用于此吗?
Thanks! 谢谢!
Module.value(key, value)
is used to inject an editable value, Module.constant(key, value)
is used to inject a constant value Module.value(key, value)
用于注入可编辑的值, Module.constant(key, value)
用于注入常量值
The difference between the two isn't so much that you "can't edit a constant", it's more that you can't intercept a constant with $provide and inject something else. 两者之间的区别不在于你“不能编辑常量”,更多的是你不能用$ provide拦截常量并注入其他东西。
// define a value
app.value('myThing', 'weee');
// define a constant
app.constant('myConst', 'blah');
// use it in a service
app.factory('myService', ['myThing', 'myConst', function(myThing, myConst){
return {
whatsMyThing: function() {
return myThing; //weee
},
getMyConst: function () {
return myConst; //blah
}
};
}]);
// use it in a controller
app.controller('someController', ['$scope', 'myThing', 'myConst',
function($scope, myThing, myConst) {
$scope.foo = myThing; //weee
$scope.bar = myConst; //blah
});
I recently wanted to use this feature with Karma inside a test. 我最近想在测试中使用此功能与Karma。 As Dan Doyon points out the key is that you would inject a value just like a controller, service, etc. You can set .value to many different types - strings, arrays of objects, etc. For example: 正如Dan Doyon指出的那样,关键是你会像控制器,服务等那样注入一个值。你可以将.value设置为许多不同的类型 - 字符串,对象数组等。例如:
myvalues.js a file containing value - make sure it is including in your karma conf file myvalues.js包含值的文件 - 确保它包含在您的业力配置文件中
var myConstantsModule = angular.module('test.models', []);
myConstantModule.value('dataitem', 'thedata');
// or something like this if needed
myConstantModule.value('theitems', [
{name: 'Item 1'},
{name: 'Item 2'},
{name: 'Item 3'}
]);
]); ]);
test/spec/mytest.js - maybe this is a Jasmine spec file loaded by Karma test / spec / mytest.js - 也许这是由Karma加载的Jasmine规范文件
describe('my model', function() {
var theValue;
var theArray;
beforeEach(module('test.models'));
beforeEach(inject(function(dataitem,theitems) {
// note that dataitem is just available
// after calling module('test.models')
theValue = dataitem;
theArray = theitems;
});
it('should do something',function() {
// now you can use the value in your tests as needed
console.log("The value is " + theValue);
console.log("The array is " + theArray);
});
});
您需要在控制器中引用csrf
IndexUsersCtrl = ( $scope, csrf )
IndexUsersCtrl.$inject = [ '$scope', 'csrf' ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.