条件无效的 ng 样式,可见性不变

ng-style with condition not working, Visibility not changing

如果获得的输出为“0”,我正在尝试 'hide' 一个元素。 我试过下面的代码行-

<li ng-style="{'visibility': {{PropertyListitem[0].Parking}} ? 'visible' : 'hidden'}">
   <p>Parking</p>
</li>

但这行不通,请大家指出我哪里出错了。

欢迎使用更简单的方法来完成相同的操作。 :)

编译器视图:

根据Angulardocumentation,同时使用插值和表达式时存在已知问题:

Note: Angular directive attributes take either expressions or interpolation markup with embedded expressions. It is considered bad practice to embed interpolation markup inside an expression

还有:

Why mixing interpolation and expressions is bad practice:
* It increases the complexity of the markup
* There is no guarantee that it works for every directive, because interpolation itself is a directive. If another directive accesses attribute data before interpolation has run, it will get the raw interpolation markup and not data.
* It impacts performance, as interpolation adds another watcher to the scope.
* Since this is not recommended usage, we do not test for this, and changes to Angular core may break your code.

TL;DR:更新您的 ng-style 如下:

ng-style="{'visibility': PropertyListitem[0].Parking ? 'visible' : 'hidden'}">

见小demo

以下应该有效:

<li ng-style="{'visibility': PropertyListitem[0].Parking ? 'visible' : 'hidden'}">
   <p>Parking</p>
</li>

您应该将对象发送到 ng-style,而不是已编译的字符串。