您好,我正在观看几个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控制器的一种方式,这个概念也适用于此吗?
谢谢!
我想在整个应用程序中共享一些变量,如基本路径.在模块配置期间,需要可以访问这些变量.我的意见是,我可以使用常量或提供者.
我有几个模块,每个模块都有自己的路由配置.在这些路由配置中,我想访问一些设置,例如.
这适用于app-module-configuration但不适用于其他模块配置(对于其他模块上的控制器),我总是得到"未知提供者:来自myApp.orders的信息".
var myApp = angular.module('myApp', ['myApp.orders']);
myApp.constant('info', {
version : '1.0'
});
myApp.config(function(info) {
console.log('app config: ' + info.version);
});
myApp.controller('MyController', function (info) {
console.log('controller: ' + info.version);
});
var orders = angular.module('myApp.orders', []);
// Remove comments to see it fail.
//orders.config(function(info) {
// console.log('orders config: ' + info.version);
//});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" class="container" ng-controller="MyController">
</div>Run Code Online (Sandbox Code Playgroud)
我想我错过了一些细节,你有什么想法吗?
javascript provider dependency-injection constants angularjs