Ng - 重复迭代这样的复杂数组

时间:2015-07-07 04:12:10

标签: angularjs angularjs-ng-repeat

我有一个像这样的数组

$ scope.disArray = [0,-5,-10,-15,[ - 11,-12,-13,-14],[ - 12,-13,-14]];

现在如果数组中的元素是直接值,那么它应该输入类型文本, 如果它是一个数组,则应该使用值数组进行选择。

期待像这样的输出

<tr>
    <td>0</td>
    <td>-5</td>
    <td>-10</td>
    <td>-15</td>
    <td>
      <select>
         <option>-11</option>
         <option>-12</option>
         <option>-13</option>
         <option>-14</option>
      </select>
    </td>
    <td>
      <select>
         <option>-11</option>
         <option>-12</option>
         <option>-13</option>
         <option>-14</option>
      </select>
    </td>

请帮助我。 提前致谢

2 个答案:

答案 0 :(得分:1)

试试这个

var app=angular.module("app",[]);

app.controller("myCtrl",function($scope){
   $scope.isArray = angular.isArray;
   $scope.disArray = [0, -5, -10, -15, [-11, -12,-13, -14], [-12, -13, -14]];

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="myCtrl">
  <table>
    <tbody>
       <tr>
          <td ng-repeat="value in disArray">
             <span ng-if="!isArray(value)">{{value}}</span>
             <select ng-if="isArray(value)" ng-options="val for val in value" ng-model="selected"></select>
    
           </td>
         </tr>
      </tbody>
    </table> 
</div>

答案 1 :(得分:0)

你可以这样做。

HTML:

<tr>
    <td ng-repeat="oneEle in disArray">
        <span  ng-if="!isArrayElement(oneEle)" ng-bind="oneEle"> </span>

        <select   ng-if="isArrayElement(oneEle)"  ng-model="oneVal"  ng-options="oneElement for oneElement in oneEle ">
        </select>    
    </td>
</tr>

JS:

$scope.disArray = [0, -5, -10, -15, [-11, -12,-13, -14], [-12, -13, -14]];

    $scope.isArrayElement = function(arrElement){
        return angular.isArray(arrElement);
    }