NG-重复在一个对象部分循环 [英] ng-repeat looping partially over an object

查看:131
本文介绍了NG-重复在一个对象部分循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个简单的程序(概念),但不幸的是。我对COM的问题和谷歌上搜索的日子已经没有导致任何东西。

我通过一个对象试图循环,它的所有儿童。我已经与这在理论上似乎pretty声防呆

两个 NG-重复 S DONE这

下面是我目前的code:

\r
\r

(函数(){\r
\r
  VAR CHEAT = angular.module('欺骗',[]);\r
  CHEAT.controller('地图',函数($范围){\r
    $ scope.search =;\r
    $ scope.range = {\r
      全局:{\r
        $:M\r
        的M:{\r
          Z:Q\r
        }\r
      },\r
      其他:{\r
        的S:{\r
          C:猫\r
        }\r
      }\r
    };\r
    $ scope.type =功能(OBJ){\r
      返回的typeof OBJ;\r
    };\r
  });\r
\r
}());

\r

&LT;脚本SRC =htt​​ps://ajax.googleapis.com/ajax /libs/angularjs/1.2.23/angular.min.js\"></script>\r
&LT; D​​IV ID =信息NG-应用=欺骗NG-控制器=地图&GT;\r
  &LT; D​​IV NG重复=(资源,道具)的范围内&GT;\r
    {{道具}} =\r
    &LT; D​​IV NG重复=(键,测试)的道具&GT;\r
      {{关键测试}}\r
    &LT; / DIV&GT;\r
  &LT; / DIV&GT;\r
&LT; / DIV&GT;

\r

\r
\r

如果您运行code,好像刚刚角刷过:

 $:M

这是几乎一样,如果只角驳回任何东西,其价值是不是一个对象。


解决方案

您已经跨越疑难杂症迷迷糊糊的角度。

这是美元符号($)开头的键不会因NG重复识别($是角和其他前端库保留字符)。

链接到 Github上的问题(目前似乎是一个做不-fix):

例如小提琴:
http://jsfiddle.net/takvg/4/

从上面提到的问题GitHub的,有来自frfancha一种解决方法:


  

解决方法很简单:只需做出Object.keys(myObject的),您的NG-重复


例如:

  $ scope.thing = {
    全局:{
        $:M
            的M:{
            Z:Q
        }
    },
        其他:{
        的S:{
            C:猫
        }
    }
};
$ scope.range = Object.keys($ scope.thing);

您会处理的是一个数组,而不是一个对象,所以你的NG-重复需要改变一点点。

I am developing a simple program (in concept) but unfortunately. I have com upon the problem and days of googling have not resulted in anything.

I'm trying to loop through an object, and all it's children. I've done this with two ng-repeats which in theory seems pretty sound and fool-proof

Here's my current code:

(function() {

  var CHEAT = angular.module('cheat', []);
  CHEAT.controller('maps', function($scope) {
    $scope.search = "";
    $scope.range = {
      "Globals": {
        "$": "M",
        "M": {
          "z": "q"
        }
      },
      "Other": {
        "S": {
          'c': "cat"
        }
      }
    };
    $scope.type = function(obj) {
      return typeof obj;
    };
  });

}());

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div id="info" ng-app="cheat" ng-controller="maps">
  <div ng-repeat="(res, prop) in range">
    {{prop}} =
    <div ng-repeat="(key,test) in prop">
      {{key}}: {{test}}
    </div>
  </div>
</div>

If you run the code, it seems like angular just brushed over the:

"$": "M"

It's almost as if angular just dismisses anything whose value isn't an object.

解决方案

You've stumbled across a gotcha in Angular.

Keys that begin with dollar sign ($) will not be recognized by an ng-repeat ($ is a reserved character in angular and other frontend libraries).

Link to the Github issue (currently seems to be a do-not-fix):

Example fiddle: http://jsfiddle.net/takvg/4/

From the github issue mentioned above, there is a workaround from frfancha:

Workaround is easy: just make your ng-repeat on Object.keys(myObject)

For instance:

$scope.thing = {
    "Globals": {
        "$": "M",
            "M": {
            "z": "q"
        }
    },
        "Other": {
        "S": {
            'c': "cat"
        }
    }
};
$scope.range = Object.keys($scope.thing);

You'll be dealing with an array instead of an object, so your ng-repeat will need to change a little bit.

这篇关于NG-重复在一个对象部分循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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