2016-09-23 63 views
0

我有一個API代碼可以生成此特定結果的文本視圖:顯示數據採用NG-重複和NG-模型數據衝突

[ 
    { 
    "devid": "3", 
    "name": "abacus", 
    "type": "math device", 

    }, 
    { 
    "devid": "4", 
    "name": "beaker", 
    "type": "science device", 
    } 
] 

現在,我可以叫我的HTML這個JSON響應文件已經在使用我放入控制器的數組。這是我的控制器代碼:

labserviceService.lab1(id) 
      .then(function(data){ 
       $scope.device = data.data; 
       $scope.deviceID = data.data[0].devid; 
       $scope.devicename = data.data[0].name; 
       $scope.devicetype = data.data[0].type; 

      }); 

如何使用ng-repeat和ng -model在我的div中顯示數據?這是我的div部分

<div class="form-group" ng-repeat="item in device"> 
     <label class="col-md-4 control-label"> 
      ID: 
     </label>             
     <div class="col-md-8">           
      <input type="text" name="regular" class="form-control" ng-model="deviceID"> 
     </div> 
     <label class="col-md-4 control-label"> 
      Name: 
     </label>             
     <div class="col-md-8">           
      <input type="text" name="regular" class="form-control" ng-model="devicename"> 
     </div> 
     <label class="col-md-4 control-label"> 
      Type: 
     </label>             
     <div class="col-md-8">           
      <input type="text" name="regular" class="form-control" ng-model="devicetype"> 
     </div> 
      <br><br> 
</div> 

我想這個代碼,但時纔會顯示重複的值是第一組,第二個是不顯示的。如何顯示json中的所有數據?非常感謝任何幫助

回答

0

嘗試類似這樣的操作。使用數組來從你的迴應得到的所有數據,例如:

$scope.device= []; 

labserviceService.lab1(id) 
       .then(function(data){ 
        for(i=0; i<data.length; i++){ 
         var myData= { 
          deviceID: data.data[i].devid, 
          devicename: data.data[i].name,         
          devicetype: data.data[i].type 
         } 
         $scope.device.push(myData); 
        }   
       }); 

然後加入NG-重複:

<div class="form-group" ng-repeat="item in device"> <!-- This is $scope.device --> 
     <label class="col-md-4 control-label"> 
      ID: item.deviceID 
     </label>             
     <div class="col-md-8">           
      <input type="text" name="regular" class="form-control" ng-model="item.deviceID"> 
     </div> 
     <!-- and so on... item.devicename item.devicetype--> 
</div> 

我希望你的幫助。我不知道你以後想做什麼...