有没有办法用来ng-if测试变量是否被定义,而不仅仅是它的真实性?
在下面的示例(实时演示)中,HTML仅显示红色项目的运费,因为item.shipping它既定义又非零.我还要显示蓝色项目的成本(已定义出货但为零),但不显示绿色项目(未定义出货).
JavaScript的:
app.controller('MainCtrl', function($scope) {
$scope.items = [
{
color: 'red',
shipping: 2,
},
{
color: 'blue',
shipping: 0,
},
{
color: 'green',
}
];
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<body ng-controller="MainCtrl">
<ul ng-repeat='item in items'>
<li ng-if='item.color'>The color is {{item.color}}</li>
<li ng-if='item.shipping'>The shipping cost is {{item.shipping}}</li>
</ul>
</body>
Run Code Online (Sandbox Code Playgroud)
我试过了ng-if='angular.isDefined(item.shipping)',但没办法.也没有ng-if='typeof(item.shipping) !== undefined'.
ABO*_*BOS 96
试试这个:
item.shipping!==undefined
Run Code Online (Sandbox Code Playgroud)
Chr*_*ann 10
我编辑了您的plunker以包含ABOS的解决方案.
<body ng-controller="MainCtrl">
<ul ng-repeat='item in items'>
<li ng-if='item.color'>The color is {{item.color}}</li>
<li ng-if='item.shipping !== undefined'>The shipping cost is {{item.shipping}}</li>
</ul>
</body>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
116086 次 |
| 最近记录: |