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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:00:14  来源:igfitidea点击:

Show ng-repeat item only when value to display is not null

javascriptangularjs

提问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 valueis 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 ngIfinstead)

(您也可以使用ngIf

Example: http://jsfiddle.net/zqJKH/

示例:http: //jsfiddle.net/zqJKH/