我的問題:如果我在[]行中的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.有活生生的例子總是有幫助。 2.我不明白你想達到什麼目的。也許你應該嘗試分解你的問題一點點 –
我清理它。我希望你能更好地理解它。謝謝! – rchiriboga
所以你想迭代'stock.price |的結果BuySellAmount:ctrl.money' in'ng-repeat =「n in [] | makeRange」',對不對? –