gpt4 book ai didi

AngularJS:ng-show/ng-hide 不适用于 `{{ }}` 插值

转载 作者:行者123 更新时间:2023-11-30 06:32:37 25 4
gpt4 key购买 nike

我正在尝试使用 AngularJS 提供的 ng-showng-hide 函数显示/隐藏一些 HTML .

根据文档,这些函数各自的用法如下:

ngHide – {expression} - If the expression truthy then the element is shown or hidden respectively. ngShow – {expression} - If the expression is truthy then the element is shown or hidden respectively.

这适用于以下用例:

<p ng-hide="true">I'm hidden</p>
<p ng-show="true">I'm shown</p>

但是,如果我们使用对象中的参数作为表达式,那么 ng-hideng-show 会被赋予正确的 true/false 值,但这些值不被视为 bool 值,因此始终返回 false:

来源

<p ng-hide="{{foo.bar}}">I could be shown, or I could be hidden</p>
<p ng-show="{{foo.bar}}">I could be shown, or I could be hidden</p>

结果

<p ng-hide="true">I should be hidden but I'm actually shown</p>
<p ng-show="true">I should be shown but I'm actually hidden</p>

这要么是错误,要么是我没有正确执行此操作。

我找不到任何关于将对象参数引用为表达式的相关信息,所以我希望对 AngularJS 有更好理解的人可以帮助我吗?

最佳答案

foo.bar 引用不应包含大括号:

<p ng-hide="foo.bar">I could be shown, or I could be hidden</p>
<p ng-show="foo.bar">I could be shown, or I could be hidden</p>

Angular expressions需要在大括号绑定(bind)中,其中 Angular directives不要。

另见 Understanding Angular Templates .

关于AngularJS:ng-show/ng-hide 不适用于 `{{ }}` 插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16625787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com