Angular JS - Can I display a module constant in HT

2019-08-06 18:50发布

I have a module with a constant like this:

angular.module('SampleMod',[])
.constant("VERSION", "2.1");

I want to show the constant value in HTML with something like this:

<p>{{VERSION}}</p>

Is it possible (without passing by the controller)?

1条回答
闹够了就滚
2楼-- · 2019-08-06 19:30

You can use $rootScope for this, but you would have to assign the value to $rootScope at some point. You could also use a directive that does essentially the same thing.

angular.module('SampleMod',[])
.constant("VERSION", "2.1")
.run(function ($rootScope, VERSION) {
  $rootScope.VERSION = VERSION;
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app=SampleMod>
<p>{{VERSION}}</p>

查看更多
登录 后发表回答