如果检查对象为空,作品有NG-节目而不是从控制? [英] Checking if object is empty, works with ng-show but not from controller?
问题描述
我已经声明,像这样一个JS对象
I have a JS object declared like so
$scope.items = {};
我也有填充的项目该对象的$ HTTP请求。我想检测此项目是空的,看来NG-显示支持这个...我输入
I also have a $http request that fills this object with items. I would like to detect if this item is empty, it appears that ng-show supports this... I enter
ng-show="items"
和神奇它的作品,我也想从控制器做同样的,但我似乎无法得到它的工作,看来我可能不得不遍历对象看它是否有任何属性或使用lodash或下划线。
and magically it works,I would also like to do the same from a controller but i can't seem to get it to work, it appears I may have to iterate over the object to see if it has any properties or use lodash or underscore.
是否有其他选择吗?
我曾尝试
alert($scope.items == true);
但它始终返回false,当创建对象时 $ HTTP
填充,所以它不工作的方式。
but it always returns false , when the object is created and when populated with $http
, so its not working that way.
推荐答案
使用一个空对象字面是没有必要在这里,你可以用null或undefined:
Use an empty object literal isn't necessary here, you can use null or undefined:
$scope.items = null;
在这种方式, NG-节目
应继续工作,并在您的控制器你可以做:
In this way, ng-show
should keep working, and in your controller you can just do:
if ($scope.items) {
// items have value
} else {
// items is still null
}
而在你的 $ HTTP
回调,你做到以下几点:
And in your $http
callbacks, you do the following:
$http.get(..., function(data) {
$scope.items = {
data: data,
// other stuff
};
});
这篇关于如果检查对象为空,作品有NG-节目而不是从控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!