javascript 仅当要显示的值不为空时才显示 ng-repeat 项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21477184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show ng-repeat item only when value to display is not null
提问by trainoasis
I just stared learning AngularJS few days ago. I have a ng-repeat on an element, but I want to use ng-show with it somehow as well. This is my code now:
几天前我刚开始学习 AngularJS。我在一个元素上有一个 ng-repeat,但我也想以某种方式使用 ng-show。这是我现在的代码:
<p ng-repeat="(parameter,value) in object">{{parameter}}: {{value}}</p>
Which works well where object is structured as
在对象结构为
{"MobilePhone":null,"Email":"[email protected]","HomePhone":null}
I want to show only elements that don't have null as a value. Something like:
我只想显示没有 null 作为值的元素。就像是:
<p ng-repeat="(parameter,value) in object" ng-show={{value}}>{{parameter}}: {{value}}</p>
But I don't see why value would be available there for ng-show already? How can I do an "If" check for value before showing
但我不明白为什么 ng-show 已经可以使用价值了?如何在显示之前对值进行“如果”检查
?
?
Thanks for any help. If I was unclear, please ask for more info.
谢谢你的帮助。如果我不清楚,请询问更多信息。
回答by CD..
This will work, and only if value
is a truly value it will be shown:
这将起作用,并且仅当value
它是真正的值时才会显示:
<p ng-repeat="(parameter,value) in obj" ng-show="value">{{parameter}}: {{value}}</p>
(You can also use ngIf
instead)
(您也可以使用ngIf
)
Example: http://jsfiddle.net/zqJKH/