2016-12-21 4 views
2

現在、私は選択入力フィールドに固執しています。私は、変数product.stockを持って、この選択の上AngularJS select/ng-options

this.quantity = { 
     values: [0,1,2,3,4,5,6,7,8,9,10], 
     decrease: function(index) { 
      if (vm.order.orders[index].quantity > 0) { 
       vm.order.orders[index].quantity--; 
       this.check(); 
      } 
     }, 
     increase: function(index) { 
      if (vm.order.orders[index].quantity < 10) { 
       vm.order.orders[index].quantity++; 
       this.check(); 
      } 
     }, 
     check: function() { 
      this.valid = false; 
      for (var i = 0; i < vm.order.orders.length; i++) { 
       if (vm.order.orders[i].quantity > 0) { 
        this.valid = true; 
       } 
       vm.order.orders[i].productId = vm.products[i].id; 
      } 
      vm.calculatePrice(); 
     } 
    } 

:それは、この「量」変数を使用していた

<select ng-init="crtController.order.orders[$index].quantity = 0" ng-model="crtController.order.orders[$index].quantity" ng-options="value for value in crtController.quantity.values track by value" ng-change="crtController.quantity.check()"></select> 

:プロジェクトでこのコードを使用しました。これが5であるとしましょう。この場合、数量配列から0,1,2,3,4,5を表示するだけです。

私は既にサーバーのものを試しました: 1. "where"節を追加しています。これはうまくいかないか、間違った方法で実装しました。 2.フィルタを追加しました。これはvalue変数とproduct.stock変数を使ってどのように動作するのかよく分かりませんが、試してみました。 3.リピートがオプションになるように変更しようとしました。これは、表示すべきではないオプションを「隠す」可能性を与えました。

<select ng-init="crtController.order.orders[$index].quantity = 0" ng-model="crtController.order.orders[$index].quantity" ng-change="crtController.quantity.check()"> 
             <option ng-repeat="value in crtController.quantity.values track by value" value="{{value}}" ng-hide="product.stock < value" ng-bind="value"></option> 
            </select> 

しかし、これは選択ボックスの最初の値が空になっているので、どちらも機能していないようです。それ以外のボタンはセレクトオプションを囲んでいます:

<div class="input-group-addon hidden-xs"><a class="min" ng-click="crtController.quantity.decrease($index)">-</a></div> 

もう動作していないようです。私も同様のStackoverflowの質問の多くを訪問したが、まだ私の解決策を見つけることができませんでした。 AngularJs conditional display for ng-options:これは私の配列にはありませんが、2つの変数のチェックだけでは機能しませんでした。私は質問を理解しますが

または

<option ng-repeat="value in crtController.quantity.values|limitTo:product.stock track by $index" value="{{value}}" ng-if="product.stock >= value" ng-bind="value"></option> 
+0

あなたのコードの例でPlunker/JSfiddle/anythingを作れますか? – valepu

答えて

3

わかりませんこれで、助けてくれるといいと思います。

+0

ニース!私のポストの最初の選択ボックスに2番目のオプションを追加すると、2番目のオプションがうまくいきます。どうもありがとうございました! – JeroenE

+0

このようなことを行う方法はありますか?limitTo:product.stock + 1?最初の配列エントリが0であるため、もう1つ追加する必要があります。 – JeroenE

+0

ofcourse。試してみる| limitTo:(product.stock + 1) –

0

の内側にあなたはオプション

<option ng-repeat="value in crtController.quantity.values track by 
value | filterQuantity " value="{{value}}" ng-bind="value"></option> 

filters.filter('filterQuantity', function() { 
    return function(tasks, tags) { 
      return tasks.filter(function(value) { 
       ... 
      }); 
     }; 
}); 

をフィルタリング試すことができればNG-だけ遊んでNG-非表示を変更しようとしていること