简体   繁体   English

ng-repeat不适用于一项

[英]ng-repeat doesn't work on one item

So I have a JSON array object that I'm doing an AngularJS ng-repeat on, the following code works when breed and/or option has more than one object, but not when they only have one object, in fact doing pet.breeds.breed[0] or pet.options.option[0] doesn't return anything when those objects only have one object 因此,我有一个正在执行AngularJS ng-repeat的JSON数组对象,当品种和/或选项具有多个对象时,以下代码有效,但当它们只有一个对象时,以下代码有效,实际上是在执行pet.breeds当那些对象只有一个对象时,.breed [0]或pet.options.option [0]不返回任何内容

  "pet": { "options": { "option": [{ "$t": "hasShots" }] }, "status": { "$t": "A" }, "contact": { "phone": {}, "state": { "$t": "NC" }, "address2": {}, "email": { "$t": "deborah@rainingpets.com" }, "city": { "$t": "Charlotte" }, "zip": { "$t": "28210" }, "fax": {}, "address1": {} }, "age": { "$t": "Adult" }, "size": { "$t": "L" }, "id": { "$t": "37778045" }, "shelterPetId": {}, "breeds": { "breed": { "$t": "German Shepherd Dog" } }, "name": { "$t": "Axel" }, "sex": { "$t": "M" }, "description": { "$t": "Axel is a handsome 4-year-old German Shepherd who is looking for a home. He is a large boy weighing 95 pounds but should be closer to 80. He has the GSD prey drive and would do best with someone who is experienced with the breed. Axel is a typical Shepherd; loving, loyal and good with children. He is also playful and does need a fenced yard to play in.\\n\\nAxel is vaccinated, neutered and micro-chipped." }, "mix": { "$t": "no" }, "shelterId": { "$t": "NC880" }, "lastUpdate": { "$t": "2017-04-08T19:31:08Z" }, "animal": { "$t": "Dog" } } 
  <div class="ui label" ng-repeat="(k, breed) in pet.breeds.breed">{{breed.$t}}</div> 

you can use angular.isArray for check it. 您可以使用angular.isArray进行检查。

 var app = angular.module('anApp', []); app.controller('ctrl', function($scope) { $scope.data = { "pet": { "breeds": { "breed": { "$t": "Siamese" } } } } $scope.isArray = angular.isArray($scope.data.pet.breeds.breed); }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.min.js"></script> <div ng-app="anApp" ng-controller="ctrl"> <div class="form-group"> <div class="ui label" ng-if="isArray" ng-repeat="(k, breed) in data.pet.breeds.breed">{{breed.$t}}</div> <div class="ui label" ng-if="!isArray" ng-repeat="(k, breed) in data.pet.breeds">{{breed.$t}}</div> </div> </div> 

Assuming that if more than one exists the structure of breeds.breed is array and is object for only one you will be best off mapping the data to consistent array structure before passing to view 假设如果breeds.breed的结构存在多个,则breeds.breed是数组并且仅是一个对象,那么在传递给视图之前,最好将数据映射到一致的数组结构

Something like: 就像是:

$http.get('path/to/api').then(function(res){
  var pet = res.data.pet;
  if(angular.isArray(pet.breeds.breed) ){
     // already an array
     $scope.breeds = pet.breeds.breed;
   }else{
     // put object in new array
     $scope.breeds = [pet.breeds.breed];
   }
})

Then in view: 然后来看:

<div class="ui label" ng-repeat="breed in breeds">{{breed.$t}}</div>

Or if you need to keep it all in the same pet object and not create new scope properties just use conditional to change the corresponding property to array if it isn't already 或者,如果您需要将其全部保留在同一个pet对象中,而不是创建新的作用域属性,则只需使用条件将相应的属性更改为数组(如果尚未设置)

 if(pet.breeds.breed && !angular.isArray(pet.breeds.breed) ){
     pet.breeds.breed  = [pet.breeds.breed];       
 }

Since you are looping object in ng-repeat key name with symbols like $ is not supported. 由于您要在ng-repeat键名中使用$符号来循环对象,因此不支持。 you need to remove the $ mark from the property 您需要从属性中删除$标记

 angular.module("app",[]) .controller("ctrl",function($scope){ $scope.obj = { "pet": { "options": { "option": [{ "$t": "hasShots" }] }, "status": { "$t": "A" }, "contact": { "phone": {}, "state": { "$t": "NC" }, "address2": {}, "email": { "$t": "deborah@rainingpets.com" }, "city": { "$t": "Charlotte" }, "zip": { "$t": "28210" }, "fax": {}, "address1": {} }, "age": { "$t": "Adult" }, "size": { "$t": "L" }, "id": { "$t": "37778045" }, "shelterPetId": {}, "breeds": { "breed": { "t": "German Shepherd Dog" } }, "name": { "$t": "Axel" }, "sex": { "$t": "M" }, "description": { "$t": "Axel is a handsome 4-year-old German Shepherd who is looking for a home. He is a large boy weighing 95 pounds but should be closer to 80. He has the GSD prey drive and would do best with someone who is experienced with the breed. Axel is a typical Shepherd; loving, loyal and good with children. He is also playful and does need a fenced yard to play in.\\n\\nAxel is vaccinated, neutered and micro-chipped." }, "mix": { "$t": "no" }, "shelterId": { "$t": "NC880" }, "lastUpdate": { "$t": "2017-04-08T19:31:08Z" }, "animal": { "$t": "Dog" } } } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="ctrl"> <div class="ui label" ng-repeat="(k, breed) in obj.pet.breeds.breed">key- {{k}} <br> value- {{breed}}</div> </div> 

Simply add below line to your controller scope, in JS file 只需在JS文件中将以下行添加到您的控制器范围

$scope.isArray = angular.isArray;

In your view file add below check 在您的视图文件中添加以下检查

<div ng-if="isArray($scope.pet.breeds)>
    -- Code for ng-repeat
</div>

 <div ng-if="!isArray($scope.pet.breeds)>
    -- Code for direct data read $scope.pet.breeds.breet.$t
</div>

I hop it helps. 我希望它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM