如何在AngularJS视图中检查空对象 [英] How to check for an empty object in an AngularJS view

查看:85
本文介绍了如何在AngularJS视图中检查空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在控制器的范围内,我有类似的内容:

In the controller's scope I have something like:

$scope.card = {};

在视图中我必须检查我的对象是否仍为空文字, {} ,或者如果它在某些字段中包含某些数据。

In the view I must check if my object is still an empty literal, {}, or if it contains some data in some fields.

我试过这个:

ng-show="angular.equals({}, card)"

ng-show="!angular.equals({}, card)"

但它不起作用。

Are还有更好的办法吗?如何检查对象是否为空或是否包含某些字段?

Are there any better ways? How do you check if an object is empty or if it contains some fields?

推荐答案

您可以使用: Object.keys(card)。length === 0

但请确保从控制器的方法中使用它作为对象在视图中不可用,例如:

But make sure you use it from a method of the controller as the Object is not available in the view, like:

$scope.isObjectEmpty = function(card){
   return Object.keys(card).length === 0;
}

然后你可以从视图中调用该函数:

Then you can call the function from the view:

ng-show="!isObjectEmpty(card)"

这篇关于如何在AngularJS视图中检查空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆