1

我正嘗試基於按鈕單擊創建動態行。行有下拉框。問題是當我添加新行並在新行中選擇一個選項時,我在前面行中選擇的選項也在改變,我的意思是前面的行選項沒有正確綁定。AngularJS:ng模型不能在多個下拉菜單中工作

我的HTML代碼:

<div id="features" ng-controller="featuresCtrl"> 
    <br> 
    <div class="row"> 
     <div class="form-group col-md-6"> 
      <table cellpadding="15" cellspacing="10"> 
       <thead> 
       <tr> 
        <th style="text-align: center;">Feature</th> 
        <th style="text-align: center;">Type</th> 
        <th style="text-align: center;">Value</th> 
        <th></th> 
        <th></th> 
       </tr> 
       </thead> 
       <tbody> 

       <tr></tr> 
       <tr ng-repeat="r in rows"> 
       <td> 
        <select ng-model="r.data.model" ng-options="option.name for option in data.availableOptions" 
          ng-change="getValues(r.data.model.name)"> 
         <option value="">Select Feature</option> 
        </select> 

        </td> 
        <td> 
         <select ng-model="r.updateData.model" ng-options="option.name for option in updateData.availableOptions" 
           ng-change="getBinSet(r.updateData.model.name)"> 
          <option value="">Select Type</option> 
         </select> 
        </td> 
        <td> 
         <select ng-model="r.binData.model" ng-options="option.name for option in binData.availableOptions"> 
          <option value="">Select Value</option> 
         </select> 
        </td> 
        <td ng-if="showAdd"> 
         <div class="text-center"> 
          <button ng-click="addRow()">Add</button> 
         </div> 
        </td> 
        <td ng-if="showAdd"> 
         <div class="text-center"> 
          <button ng-click="remove($index)">Remove</button> 
         </div> 
        </td> 
       </tr> 
       <tr> 
        <td></td> 
        <td style="align-self: center;"> 
         <button style="align-self: center" ng-click="submitForm(rows)">Submit</button> 
        </td> 
        <td></td> 
       </tr> 

       </tbody> 
      </table> 
      <br> 
     </div> 

我的角度JS代碼:

'use strict'; 

var testControllers = angular.module('testControllers'); 

testControllers.controller('featuresCtrl', ['$scope','$route','$filter', '$http', '$location','$window','$timeout', 'NgTableParams', 
    function($scope, $route, $filter, $http, $location, $window, $timeout, NgTableParams) { 

     $scope.rows = [{}]; 
     $scope.nrows = []; 
     $scope.showAdd = false; 


     $scope.addRow = function() { 
      $scope.rows.push({ 

      }); 

     }; 

     $scope.remove = function(index) { 
       $scope.rows.splice(index, 1); 
     }; 

     $scope.submitForm = function(rows) { 
      console.log("rows", rows); 
     }; 

     $scope.data = { 
      model: null, 
      availableOptions: [ 
       { name: 'TestA'}, 
       { name: 'TestB'}, 
       { name: 'TestC'}, 
       { name: 'TestD'} 

      ] 
     }; 

     $scope.getValues = function (featureType) { 
      console.log("getValues", featureType); 

      $scope.showAdd = false; 


      if (featureType != null) { 

       $http.get('/getPropensityValues.do', {params: {'featureType': featureType}}).success(function (data) { 
        var test = []; 
        angular.forEach(data, function (item) { 
         test.push({name: item}); 
        }); 
        $scope.updateData = { 
         model: null, 
         availableOptions : test 
        }; 
       }); 
      } 
     }; 

     $scope.getBinSet = function (featureType) { 
      console.log("getBinSet", featureType); 

      $scope.showAdd = true; 

      if (featureType != null) { 
       $scope.binData = { 
        model: null, 
        availableOptions: [ 
         {name: '1'}, 
         {name: '2'}, 
         {name: '3'}, 
         {name: '4'}, 
         {name: '5'}, 
         {name: '6_10'}, 
         {name: '10_15'}, 
         {name: '16_20'}, 
         {name: '21_25'}, 
         {name: '26_30'}, 
         {name: '31_35'}, 
         {name: '36_40'}, 
         {name: '41_45'}, 
         {name: '46_50'}, 
         {name: '>50'} 
        ] 
       }; 

      } 
     }; 

    }]); 

有人能告訴我什麼,我做錯了什麼?我試着用另一個例子 - https://plnkr.co/edit/Jw5RkU3mLuGBpqKsq7RH?p=preview 即使在這裏,我也面臨同樣的問題,當選擇第2行第1行也改變。使用r.data.model來綁定每一行的值是否是錯誤的?

回答

1

我更新了plunker工作:

https://plnkr.co/edit/4sJHHaJPEuYiaHjdnFBp?p=preview

你沒有定義什麼模式是當你重新定義選項菜單(旁註:你應該在一個充分利用underscore.js的difference功能過濾器來創建動態相應的顯示選項)

不管怎樣,在你的addRow()函數,我改變了它從這個:

if(val=== 'TestB') { 
    $scope.data1 = { 
     model: null, 
     availableOptions: [ 
      { name: 'TestA'}, 
      { name: 'TestC'}, 
      { name: 'TestD'} 

     ] 
    }; 
} 

這樣:

if(val=== 'TestB') { 
    $scope.data1 = { 
     model: 'TestB', 
     availableOptions: [ 
      { name: 'TestA'}, 
      { name: 'TestC'}, 
      { name: 'TestD'} 

     ] 
    }; 
} 

讓我知道,如果這有助於

UPDATE:

我重新從頭開始的功能,因爲我覺得你得太多了的東西。這裏所需要的只是非常簡單的ng-repeat,ng-options和ng-model綁定。

<tr ng-repeat="r in rows track by $index"> 
    <td> 
     <select ng-model="r.name" 
       ng-options="option.name as option.name for option 
             in availableOptions"> 
      <option value="">Select Value</option> 
     </select> 
    </td> 
    <td> 
     <input type="button" ng-click="addRow()" value="Add"> 
    </td> 
    <td> 
     <input type="button" ng-click="deleteRow($index)" 
      value="Delete"> 
    </td> 
</tr> 

和角

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

app.controller('MainCtrl', function($scope) { 

    $scope.rows = [{name:""}]; 

    $scope.addRow = function(){ 
    $scope.rows.push({name:""}); 
    } 

    $scope.availableOptions = [ 
       { name: 'TestA'}, 
       { name: 'TestB'}, 
       { name: 'TestC'}, 
       { name: 'TestD'} 

      ]; 

    $scope.deleteRow = function(index){ 
    $scope.rows.splice(index,1); 
    } 
}); 

我沒有差別的東西扔呢,但應該很容易。

+0

它仍然沒有約束力。當我在新行中選擇添加和選擇選項時,第一行也會更改。 – user3407267

+0

你能讓我知道我在做什麼我的代碼錯了嗎?我的掠奪者只是我的一次嘗試。我試圖讓它起作用的代碼是在 – user3407267

+0

的問題好吧,我做了一個比原來簡單得多的新plunkr,因爲我認爲你確實在推翻它。 https://plnkr.co/edit/4sJHHaJPEuYiaHjdnFBp?p=preview – Chris