Nik*_* So 87 javascript angularjs
您好,我正在观看几个angular.js视频,并看到value()方法用于设置一种模块范围的常量.例如,可以像这样设置Angular-UI库的配置:(coffeescript)
angular.module('app',[])
.value "ui.config",
tinymce:
theme: 'simple'
width: '500'
height: '300'
Run Code Online (Sandbox Code Playgroud)
我的应用程序目前看起来像这样:
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']
Run Code Online (Sandbox Code Playgroud)
但是我似乎无法通过点击与app模块相对应的'app'变量来获得该值.
我在这里读到了ST和在angularjs的google小组上,通过服务共享公共代码btwn控制器的一种方式,这个概念也适用于此吗?
谢谢!
Ben*_*esh 147
Module.value(key, value)用于注入可编辑值,
Module.constant(key, value)用于注入常量值
两者之间的区别不在于你"不能编辑常量",更多的是你不能用$ 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
});
Run Code Online (Sandbox Code Playgroud)