AngularJs:NG-显示/ NG隐藏前pression未评估AngularJs、NG、pression

2023-09-13 04:31:54 作者:老爷子@

我是新来的JavaScript和AngularJS,不知道为什么在引号中的前pression没有评估?

I'm new to both javascript and AngularJS, and wondering why is the expression inside the quotes not evaluated?

<span ng-show="{{remaining()}}!==0">sometext</span>

这只不过是打印这样的:

it is simply printed like this:

<span ng-show="2!==0">sometext</span>

和NG-节目是不是不管内容合作。即使前pression被包裹在一个eval文本(和印刷前pression)所示,

and the ng-show is not working regardless of the contents. The text ( and the printed expression) is shown even if the expression is wrapped in an eval, :

eval("{{remaining()}}!==0")

我使出我的控制器为此创建一个函数:

I resorted to creating a function in my controller for this:

<span ng-show="renderOrNot()">sometext</span>

这工作,但我会preFER不要有我想要做一个比较,每次写一个函数

which works, but I would prefer not to have to write a function each time I want to make a comparison

推荐答案

几乎没有...

当您使用 {{}} ,该值是插入,即标记将被替换为前pression的结果。 ngShow预计只有前pression,因此就使用功能,因为它是,它可以工作:

When you use {{}}, the values are interpolated, i.e. the markup is replaced with the result of the expression. ngShow expects only the expression, so just use the function as it is, and it will work:

<span ng-show="remaining() !== 0">sometext</span>

在一般情况下,你只需要 {{}} 时,应显示你的前pression /内容。

In general, you'll only want {{ }} when your expression / content should be displayed.