Angular JS-我可以用HTML显示模块常量吗? [英] Angular JS - Can I display a module constant in HTML?

查看:121
本文介绍了Angular JS-我可以用HTML显示模块常量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有这样常数的模块:

I have a module with a constant like this:

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

我想用这样的方式在HTML中显示常量值:

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

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

是否可能(无需通过控制器)?

Is it possible (without passing by the controller)?

推荐答案

您可以为此使用 $ rootScope ,但是必须将值分配给 $ rootScope 。您还可以使用基本执行相同操作的指令。

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>

这篇关于Angular JS-我可以用HTML显示模块常量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆