为JavaScript对象空NG重复angularjs默认文本 [英] angularjs default text for empty ng-repeat on javascript object

查看:107
本文介绍了为JavaScript对象空NG重复angularjs默认文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题让默认的文本打印出来,即遍历一个JavaScript对象的空NG重复。通常情况下,我只想做一个< D​​IV NG秀=object.length == 0>空集< / DIV>中,但你不能叫长度。JavaScript对象上我有一个简单的jsfiddle来演示该问题: http://jsfiddle.net/C4t4LystX/zHJv8/8/ 。基本上我只需要知道,当NG重复没有对象重复过,所以我可以显示任何帮助一些默认的文本。谢谢。

I have a problem getting default text to print out for an empty ng-repeat that is iterating over a javascript object. Normally I would just do a <div ng-show="object.length==0">EMPTY SET</div>" but you can't call length on a javascript object. I have a simple jsfiddle to demonstrate the problem: http://jsfiddle.net/C4t4LystX/zHJv8/8/. Basically I just need to know when the ng-repeat has no objects to repeat over so I can show some default text. Thanks for any help.

推荐答案

您是正确的,有在JavaScript没有object.length。因为你是复位源空对象,你需要检查,看看如果对象是空的,或者有一些消息来源。您可以编写一个名为的isEmpty简单的功能。

You are correct, there is no object.length in javascript. Because you are resetting sources to an empty object, you need to check to see if the object is empty, or has some sources. You can write a simple function called isEmpty.

   <div ng-show="isEmpty(sources)">
          EMPTY SOURCES
   </div>

function Ctrl1($scope) {
    $scope.sources = {source1:{id:"source1"},source2:{id:"source2"}};

    $scope.cleanSources = function(){
        $scope.sources = {};                               
    };

    $scope.isEmpty = function (obj) {
       return angular.equals({},obj); 
    };
}

http://jsfiddle.net/zHJv8/21/

修改:改变的isEmpty使用angular.equals而不是每个回路

EDIT: Changed isEmpty to use angular.equals instead of for each loop.

这篇关于为JavaScript对象空NG重复angularjs默认文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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