Angular JS - Can I display a module constant in HTML?

两盒软妹~` 提交于 2019-12-13 00:14:04

问题


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:


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>


来源:https://stackoverflow.com/questions/27678465/angular-js-can-i-display-a-module-constant-in-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!