2016-06-21 70 views
0

我的問題:如果我在[]行中的ng-repeat =「n中放置了一個整數[],它可以工作,但我想要的是能夠使用stock.price | BuySellAmount:ctrl.money這是一個自定義過濾器,基本上將人的總金額除以該股的價值。但是當我只是添加stock.price | BuySellAmount:在[]中的ctrl.money我得到一個錯誤。那麼如何在[]中添加該信息?在ng-repeat中獲取最大數量的可購買產品

  <div ng-if="stock.price | BuySellAmount:ctrl.money" class="input-group col-md-1"> 
       <select class="form-control "> 
       <option ng-repeat="n in [] | makeRange" ng-selected= "{{n == stock.price | BuySellAmount:ctrl.money}}">{{n}}</option> 
       </select>    
       <span class="input-group-btn"> 
       <button ng-if="stock.price | BuySellAmount:ctrl.money" class="btn btn-success" ng-click="buyStock()">Buy</button> 
       </span> 
      </div> 



(function() { 
     'use strict'; 
     angular 
     .module('MODULE') 

     // controller 
     .controller('CONTROLLER',['$http', function($http){ 
      var self = this; 

      self.money = 50; 

      // get the json feed and create a new array with the prices and labels 
      $http.get('js/stocks.json') 
      .success(function(data, status, headers, config) { 
       //self.stockList = angular.copy(data.stocks); 
       //console.log(self.stockList); 
       var stocks = []; 
       for (var i = 0; i < data.stocks.length; i++) { 
        stocks.push({ 
         name: data.stocks[i].name, 
         price: Math.floor(Math.random()*(600-data.stocks[i].price+1)+data.stocks[i].price) 
        }); 
       } 
       self.stockList = stocks; 
      }) 
      .error(function(data, status, headers, config) { 
       // log error 
      }); 


     }]) 
     .filter('randomPrice', function() { 
     return function(min, max) 
     { 
      var max = 5000; 
      console.log() 
      return Math.floor(Math.random()*(max-min+1)+min); 
     } 
    }) 


    .filter('BuySellAmount', function() { 
     return function(stockprice,moneyinhand) 
     { 
      //Math.random()*(max-min+1)+min 
      var amount = Math.floor(moneyinhand/stockprice); 
      return amount; 
     } 
    }) 

    .filter('makeRange', function() { 
     return function(input) { 
      var lowBound, highBound; 
      switch (input.length) { 
      case 1: 
       lowBound = 0; 
       highBound = parseInt(input[0]) - 1; 
       break; 
      case 2: 
       lowBound = parseInt(input[0]); 
       highBound = parseInt(input[1]); 
       break; 
      default: 
       return input; 
      } 
      var result = []; 
      for (var i = lowBound; i <= highBound; i++) 
       result.push(i); 
      return result; 
     }; 
    }); // end 
    })(); 
+1

1.有活生生的例子總是有幫助。 2.我不明白你想達到什麼目的。也許你應該嘗試分解你的問題一點點 –

+0

我清理它。我希望你能更好地理解它。謝謝! – rchiriboga

+0

所以你想迭代'stock.price |的結果BuySellAmount:ctrl.money' in'ng-repeat =「n in [] | makeRange」',對不對? –

回答

0

對於那些可能有此問題的人。我希望不要太難遵循,我能夠把stock.price | BuySellAmount:內的{{}}中的[]使用()代替ctrl.money的NG-重複

新的NG-重複

<div ng-if="stock.price | BuySellAmount:ctrl.money" class="input-group col-md-1"> 
       <select class="form-control "> 
       <option ng-repeat="n in [(stock.price | BuySellAmount:ctrl.money)] | makeRange" ng-selected= "{{n == stock.price | BuySellAmount:ctrl.money}}">{{n}}</option> 
       </select>    
       <span class="input-group-btn"> 
       <button ng-if="stock.price | BuySellAmount:ctrl.money" class="btn btn-success" ng-click="buyStock()">Buy</button> 
       </span> 
      </div> 
+1

你做'stock.price | BuySellAmount:ctrl.money'三次,而它只能完成一次。你應該小心使用過濾器,因爲它們是在每個摘要上評估的。在這裏找到如何優化你的代碼:[SO回答](http://stackoverflow.com/a/11722324/1065780) –

相關問題