AngularJs :将常量注入控制器 - 如何在控制器视图中访问它?



app.js中,我有

(function ()
{
angular.module('routerDemo',
[
'ui.router',   // Routing
])
})();
angular.module('routerDemo').constant('constants',  { blip: 'blop', blup: 307 });
angular.module('routerDemo').constant('another_constant',  42);

和在controller_1.js

Controller_1.$inject = ['$rootScope', '$state', 'constants', 'another_constant'];

function Controller_1(   $rootScope,   $state,   constants,   another_constant)
{

并且在视图中

<div ng-controller="Controller_1 as controller_1">
<h1>This is view 1</h1>
<div>Controller_1.constants.blip == {{controller_1.constants.blip}}</div>
<div>Controller_1.the_answer == {{controller_1.another_constant}}</div>

但它们显示为空白(尽管如果我在控制器中断点,我可以查看它们,所以这只是视图中的语法问题(。

我做错了什么?

只需将常量分配给控制器中的this

// controller_1
function Controller_1($rootScope, $state, constants, another_constant){
this.constants = constants;
this.another_constant = another_constant;
}
// view
<div ng-controller="Controller_1 as controller_1">
<h1>This is view 1</h1>
<div>Controller_1.constants.blip == {{controller_1.constants.blip}}</div>
<div>Controller_1.the_answer == {{controller_1.another_constant}}</div>
</div>

相关内容

  • 没有找到相关文章

最新更新