繁体   English   中英

如何在ng-options中添加两个索引并使用Angular.js动态设置值

[英]How to add two index in ng-options ans set the value dynamically using Angular.js

我需要一个帮助。我需要使用Angular.js在ng-options对象中为两个索引( ie-$index,$parent.$index )。我在下面解释我的代码。

<tr ng-repeat="d in days">
        <td>{{d.day_name}}</td>
        <td>
          <table>
            <tbody>
              <tr ng-repeat="answer in d.answers">
                <td>
                  <select class="form-control" id="answer_{{$index}}_{{$parent.$index}}_category" name="answer_{{$index}}_category" ng-model="answer.catagory" ng-options="cat.name for cat in listOfCatagory track by cat.value" ng-change="removeBorder($index,answer.catagory.value,$parent.$index);">
                    <option value="">Select Category</option>
                  </select>
                </td>
                 </tr>
            </tbody>
          </table>
          </td>
          <td>
          <table>
            <tbody>
              <tr ng-repeat="answer in d.answers">
                <td>
                  <select class="form-control" id="answer_{{$index}}_{{$parent.$index}}_subcategory" name="answer_{{$index}}_subcategory" ng-model="answer.subcatagory" ng-options="sub.name for sub in listOfSubCatagory[$index+$parent.$index] track by sub.value">
                    <option value="">Select Subcategory</option>
                  </select>
                </td>
                </tr>
            </tbody>
          </table>
          </td>
          <td>
          <table>
            <tbody>
              <tr ng-repeat="answer in d.answers">
                <td>
                  <input type="text" id="answer_{{$index}}_{{$parent.$index}}_comment" name="answer_{{$index}}_comment" placeholder="Add Comment" ng-model="answers.comment">
                </td>

          </tr>
            </tbody>
          </table>
          </td>
          <td>
          <table>
            <tbody>
              <tr ng-repeat="answer in d.answers">
                <td>
                    <input type="submit" name="plus" id="plus" value="+" style="width:20px; text-align:center;" ng-click="addNewRow(d.answers, true)">
                    <div ng-show="$first && !$last">
                     <input type="submit" name="minus" id="minus" value="-" style="width:20px; text-align:center;" ng-click="removeRow(d.answers, $last)">
                     </div>
                </td>
          </tr>

在这里,我需要传递两个带有listOfSubCatagory索引,以便我可以动态设置多维数据。我做了一些编码,但它不能按照要求工作。我在下面解释我的控制器端代码。

$scope.listOfSubCatagory = []; 
  $scope.removeBorder=function(index,catvalue,parent){
      console.log('ind',index,catvalue,parent);
     $scope.listOfSubCatagory[index+parent]=[];
     var catdata=$.param({'action':'subcat','cat_id':catvalue});
        $http({
            method:'POST',
            url:"php/customerInfo.php",
            data:catdata,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).then(function successCallback(response){
            angular.forEach(response.data,function(obj){
                var data={'name':obj.subcat_name,'value':obj.subcat_id};
                $scope.listOfSubCatagory[index+parent].push(data);
            })
        },function errorCallback(response) {
        }) 
  }

$scope.days=[];
    $http({
        method:'GET',
        url:"php/customerInfo.php?action=day",
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }).then(function successCallback(response){
        //console.log('day',response.data);
         angular.forEach(response.data, function(obj) {
      obj.answers = [];
      $scope.addNewRow(obj.answers);
      $scope.days.push(obj);
    });
    },function errorCallback(response) {
    })
    }
     $scope.addNewRow = function(answers, hasDelete) {
    answers.push({
      category: null,
      subcategory: null,
      comment: null,
      hasDelete: hasDelete ? hasDelete : false
    });
  };

  $scope.removeRow = function(answers, $index){
    answers.splice($index, 1);
  };

请帮助我解决此问题。

我已经为您更新了我的代码。 如果使用类似的方法,则可能会得到所需的结果。 对不起,我早先plunk了拼写错误。 我现在改进了它们。 您可以进行相应的更新并更新代码,

https://plnkr.co/edit/Px7vY4?p=预览

暂无
暂无

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

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