Angularjs ng if并检查是否存在非空、非null对象
我正在尝试这个:Angularjs ng if并检查是否存在非空、非null对象,angularjs,select,angular-ng-if,Angularjs,Select,Angular Ng If,我正在尝试这个: <select ng-disabled="!items" class="form-control" ng-model="selected" ng-options="key as value for (key , value) in items"> <option ng-if="items" value="">All Items</option> </select> 所有项目 但是当有项目时,All Items选项不显
<select ng-disabled="!items" class="form-control" ng-model="selected" ng-options="key as value for (key , value) in items">
<option ng-if="items" value="">All Items</option>
</select>
所有项目
但是当有项目时,All Items
选项不显示
注意:问题是由于ng造成的,如果,则没有其他原因
演示:您可以像这样检查对象id是否为空
$scope.isEmpty = function (obj) {
for (var i in obj) if (obj.hasOwnProperty(i)) return false;
return true;
};
在视图中,您可以执行以下操作:
<option ng-show="!isEmpty(items)">All Items</option>
所有项目
ng if在选项标记中不起作用——这是因为select元素在DOM树上的位置高于带有ng if的null选项,因此它将首先编译。
这对我来说是个不错的选择:
angular
.module('myApp')
.filter('empty',function(){
返回函数(obj){
返回obj==null | | angular.equals({},obj)
}
})
0项
添加函数作为过滤器,并使用Angular进行测试
感谢@Hmahwish的启发 您可以在
@SergioTulentsev上安装ng repeat
,而不是ng options
,我知道,但这个问题是关于ng if
替代方案,或者一种不使用ng repeat
而不是ng options
来解决此问题的方法。谢谢或者,只需将您的虚拟选项添加到items
arrayYou可以使用ng show
作为ng if
的备选方案,您的意思是什么,“ng if
备选方案”?这使它成为一个我觉得在angular或jquery中应该有一个函数来实现这一点。不是吗?在angular中可能有angular.equals({},$scope.items)