如何在AngularJS上的HTML中直接访问模块的常量 [英] How to directly access module's constant in HTML on AngularJS
本文介绍了如何在AngularJS上的HTML中直接访问模块的常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想直接在html中使用多个常量(在控制器中使用几次).
I want to use several constants directly in html (and few times in controllers).
例如,这是主要的应用模块:
For example, this is main app module:
angular.module('website', [])
.constant('ROUTES', (function () {
return {
SIGN_IN: '/sign/in'
}
})())
.config([..., 'ROUTES', function(..., ROUTES {
$routeProvider.when(ROUTES.SIGN_IN, {templateUrl: 'pages/sign_in.html', controller: 'SignInController'});
}]);
很明显,如何使用控制器中的常量.
So this is clear, how to use constants from controllers.
但是我该怎么做:
<html ng-app="website">
<body>
<a href="{{ROUTES.SIGN_IN}}">Sign in</a>
</body>
</html>
重点是将所有路线都放在一个地方. 所以,我可以这样做,还是我选择了错误的方式?
The point is to keep all routes in one place. So, can i do this, or may be i choosed wrong way?
推荐答案
IMHO the better way to do this is use the $rootScope In html every scope inherits from the $rootScope, so if a variable isn't present in the current scope angular use the one declared in $rootScope.
一种好的(IMHO)方法是在 run 阶段"
A good (IMHO) way is to initialize this in the run "phase"
angular.module('myApp')
.run(function ($rootScope) {
$rootScope.ROUTES = ROUTES
});
这篇关于如何在AngularJS上的HTML中直接访问模块的常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文