2015-12-01 164 views
0

我需要刪除此空或設置默認值,因爲當我發帖的形式向API我沒有在陣列中的所有元素刪除空白或設置默認選項,但我需要它,因爲沒有選擇的選擇,他們沒有傳遞給JSON。角NG選項特殊選擇設置

here我找到答案,但這裏是簡單的例子。 我有更復雜的選擇,我不知道如何重拍這個。 我的選擇是表,其中TD是動態生成的,看起來像這樣:

<tr ng-repeat="articleItem in articleColors"> 
    <td><button class="btn btn-default"><i class="fa fa-plus"></i></button></td> 
    <td>{{ articleItem.nazwa_art }}</td> 
    <td>{{ articleItem.kolory_pod }}{{ articleItem.pantony }}</td> 
    <td ng-repeat="m in [] | rangeFilter:0:pyNumber"> 
     <select id="articleColorsData" name="py{{$index+1}}" class="form-control" ng-model="articleColorsData.kolory[$index]"> 
      <option value="x" selected>x</option> 
      <option ng-repeat="oneColor in articleItem.all_colors" ng-selected="" value="{{ oneColor }}">{{ oneColor }}</option> 
     </select> 
    </td>           
</tr> 

我和我的選擇與NG選項很努力,但它不能正常工作。我有$ index是未定義的錯誤。 我做了這樣的事情:

<select id="articleColorsData" name="py{{$index+1}}" 
ng-model="articleColorsData.kolory[$index]" 
ng-options="oneColor.value as oneColor.name for oneColor in articleItem.all_colors"></select> 

而且在cotroller:

$scope.articleColorsData.kolory[$index] = $scope.articleItem.all_colors[0].value; 

所有代碼形式控制器:

appPokayoke.controller('CreateArticleColorsCtr', ['$scope', '$http','$routeParams', 'articles', '$location', 'articlesOfDrawings', function ($scope, $http, $routeParams, articles, $location, articlesOfDrawings, rangeFilter) { 


     var drawingId = $routeParams.drawingId; 
     var pyNumber = $routeParams.pyNumber; 
     var articleName = $routeParams.articleName; 



     $scope.pyNumber = $routeParams.pyNumber; 

     $scope.articleColors = {}; 
     $scope.articleColorsData = { 
     "id_rys" : drawingId, 
     "ile_py" : pyNumber, 
     "nazwa_art" : articleName 
     }; 

     $scope.articleColorsData.kolory[$index] = $scope.articleItem.all_colors[0].value; 

     articles.getNewArticle(
      $routeParams.articleName, 
      $routeParams.drawingId, 
      function (data) { 
       if(data !== '') { 
        $scope.articleColors = data; 

       } 
       else { 
        console.log('not ok articleColors'); 
       } 
      } 
     ); 
    }]); 

回答

0

我所做的第二件事 - 設置默認值,這是對我來說更好的方式,但空白仍然存在。代碼與我在開始時的代碼相同。 我只加選擇此代碼:

ng-init="articleColorsData.kolory[$index] = 'x'" 

我全碼:

<td ng-repeat="m in [] | rangeFilter:0:pyNumber"> 
    <select id="articleColorsData" name="py{{$index+1}}" class="form-control" ng-model="articleColorsData.kolory[$index]" ng-init="articleColorsData.kolory[$index] = 'x'"> 
     <option ng-repeat="oneColor in articleItem.all_colors" ng-value="x" value="{{ oneColor }}">{{ oneColor }}</option> 
    </select> 
</td>