0

My issue: If i put an interger inside [] from the ng-repeat="n in []" line, it works, but what I want is to be able to use stock.price | BuySellAmount:ctrl.money which is a custom filter that basically divides the total amount of money a person has, by the amount the stock is worth. but when I just add stock.price | BuySellAmount:ctrl.money within the [] i get an error. So how can I achieve addding that information within the []

            <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
    })();
rchiriboga
  • 187
  • 2
  • 16
  • 1
    1. Having live example always helps. 2. I don't understand what you try to achieve. Maybe you should try to decompose your question a little bit – Serhii Holinei Jun 21 '16 at 18:00
  • I Cleaned it up. I hope you can understand it better. thanks! – rchiriboga Jun 21 '16 at 18:13
  • So you want to iterate over result of `stock.price | BuySellAmount:ctrl.money` in `ng-repeat="n in [] | makeRange"`, right? – Serhii Holinei Jun 21 '16 at 18:20
  • yea I just figured it out. I was getting an error when I did {{stock.price | BuySellAmount:ctrl.money}} but I did (stock.price | BuySellAmount:ctrl.money) inside the array brackets and I got it to work. so now the select only shows me the amount from 0 to X based on the amount of stocks that the person is able to buy. – rchiriboga Jun 21 '16 at 18:22

1 Answers1

0

For those that may have this issue. which I hope isn't too hard to follow, I was able to put the stock.price | BuySellAmount:ctrl.money within the [] using () instead of {{}} within the 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>
rchiriboga
  • 187
  • 2
  • 16
  • 1
    You do `stock.price | BuySellAmount:ctrl.money` three times, whereas it can be done only once. You should be careful with filters, because they are evaluated on each digest. Find how you can optimise your code a little bit here: [SO answer](http://stackoverflow.com/a/11722324/1065780) – Serhii Holinei Jun 21 '16 at 18:28