获得ng-repeat内可购买物品的最大数量

时间:2016-06-21 17:45:27

标签: angularjs angularjs-ng-repeat

我的问题:如果我在[]"中的ng-repeat =" n中将[]放入[]中。线,它的工作原理,但我想要的是能够使用 stock.price | BuySellAmount:ctrl.money 这是一个自定义过滤器,它基本上将一个人的总金额除以股票的价值。但是当我只是添加stock.price | BuySellAmount:[]中的ctrl.money我得到一个错误。那么我怎样才能在[]

中添加这些信息
            <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 个答案:

答案 0 :(得分:0)

对于那些可能有这个问题的人。我希望它不会太难以遵循,我能够把股票。价格| BuySellAmount:[]中的ctrl.money使用()而不是ng-repeat中的{{}}

new 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 [(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>