2017-05-27 100 views
1

循環遍歷ng-repeat很容易,因爲您擁有json對象並通過鍵可以循環和收集數據。 這裏我的問題是如何循環通過特定範圍或輸入來獲取包含到ng重複值但未包含到json對象中的值?使用Angularjs循環遍歷範圍

這裏是全碼:https://jsfiddle.net/medoo/wgc1my7d/

解釋

我有很簡單的對象,而像這樣的

var arr = ["~\\191746.JPG", "~\\191848.JPG", "~\\191908.JPG"]; 

鍵之後,我列出了在結構簡單的表作爲遵循這些價值觀

<div data-ng-app="myApp" data-ng-controller="myCtrl"> 
     <table id="fils"> 
      <tr data-ng-repeat="oneFile in ShowFiles"> 
       <td style="border: 1px solid #000000">{{oneFile}}</td> 
       <td style="border: 1px solid #000000"> 
        <input data-ng-model="naming" type="text" style="width: 200px" /> 
        <%--here is my problem !!! 
        i need to get values of element "input" or scope "naming" which not included with ShowFiles --%> 
       </td> 
      </tr> 
     </table> 
     <input id="save" data-ng-click="save()" type="button" value="Save" /> 
     <div id="msg"></div> 
    </div> 


    <script> 
    var arr = ["~\\191746.JPG", "~\\191848.JPG", "~\\191908.JPG"]; 
    var app = angular.module("myApp", []); 
    app.controller("myCtrl", function ($scope) { 
     $scope.ShowFiles = arr; 

     $scope.save = function() { 
      var msg = document.getElementById("msg"); 
      var index = 0; 
      $scope.ShowFiles.forEach(function (oneFile, naming) { 
       msg.innerHTML = 
       msg.innerHTML + 'row #' + (index++) + ': ' + JSON.stringify(oneFile) + ' --- ' + naming + '<br />'; 
      }); 
     }; 
    )}; 
    </script> 

我所需要的,當我按下按鈕#save收集的所有數據,兩列是否
(納入範圍「ShowFiles」如{{oneFile}})
OR
(未納入範圍「ShowFiles」這樣的爲[輸入數據-NG-模型= 「命名」]) - 「這裏問題」

要被顯示爲以下

oneFile ---命名
行#0:「〜\ 191746.JPG「---」動物「
行#1:」〜\ 191848.JPG「--- 「汽車總動員」
列#2: 「〜\ 191908.JPG」 --- 「朋友」

但可惜的命名顯示這樣的0,1,2

回答

0

序列號,以使這項工作,你需要與此更換您的輸入元素標籤:

<input data-ng-model="fileNames[$index]" type="text" style="width: 200px" /> 

和控制器代碼如下:

var arr = ["~\\191746.JPG", "~\\191848.JPG", "~\\191908.JPG"]; 
var app = angular.module("myApp", []); 
app.controller("myCtrl", function ($scope) { 
    $scope.ShowFiles = arr; 
    $scope.fileNames = []; 

    $scope.save = function() { 
     var msg = document.getElementById("msg"); 
     $scope.ShowFiles.forEach(function (oneFile, index) { 
      msg.innerHTML = 
      msg.innerHTML + 'row #' + (index + 1) + ': ' + JSON.stringify(oneFile) + ' --- ' + $scope.fileNames[index] + '<br />'; 
     }); 
    }; 
});