1

Can someone explain why is the $http request sending infinite number of requests to the server? In my application, this code sends infinite requests to the server

(function(){
    var app = angular.module("GrahamsSocksProducts", ["ngCookies"]);
    app.controller("ProductsController", ["$controller", "$http", "$cookies", function($controller, $http, $cookies){
.
.
.
this.setCookie = function(){
        username = "Some random guy"
        alert(45)

        $http({
            method : "GET",
            url : "http://_____________",
            params : { username : username }
        }).then(function(){
            //do something
        })
    }

However, when I remove the http request, only a single request is passed to the server, like this :

.
.    
.
this.setCookie = function(){
        username = "Some random guy"
        alert(45)
}
.
.
.

HTML :

<div ng-app="GrahamsSocksProducts">
        <div ng-controller="ProductsController as products">
        {{ products.setCookie() }}
            <div class="row">
                <div ng-repeat="product in products.products">
                    <div class="col-sm-4 col-lg-4 col-md-4">
                        <div class="thumbnail">
                            <img ng-src="{{products.getImageTag(product)}}">
                            <div class="caption">
                                <h4 class="pull-right">${{ product.price }}</h4>
                                <h4>
                                    <a href="#"> {{ product.name }}</a>
                                </h4>
                                <p>{{ product.description }}</a>.</p>
                                </div>
                    </div>
                    <div class="ratings">
                        <p class="pull-right">{{ product.ratings }} / 5</p>
                        <p>
                                <span ng-class="products.getStarColor(product.ratings, 1)" ng-click="product.ratings=1"></span>
                                <span ng-class="products.getStarColor(product.ratings, 2)" ng-click="product.ratings=2"></span>
                                <span ng-class="products.getStarColor(product.ratings, 3)" ng-click="product.ratings=3"></span>
                                <span ng-class="products.getStarColor(product.ratings, 4)" ng-click="product.ratings=4"></span>
                                <span ng-class="products.getStarColor(product.ratings, 5)" ng-click="product.ratings=5"></span>
                        </p>
                    </div>
                    <div>
                        <button class="btn btn-primary" ng-click="products.addToCart(product)">Add to Cart</button>
                        <button class="btn btn-default">Checkout</button>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="container">                 
                        <div class="jumbotron">
                            <ul class="list-group"ng-repeat="cartProduct in products.getCartNameAndQuantity()">
                                <li class="list-group-item">{{ cartProduct.name }}  ({{ cartProduct.quantity }})</li>
                            </ul>
                            Cart products price : {{ products.getCartPrice() }}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

I have read other questions such as this: Infinite loop when trying to make Angularjs display a promise They have answered about how AngularJS allows two-way data-binding but in this case, when the $http code is removed, the request is sent once, otherwise infinite times.

1 Answer 1

4

The expression {{ products.setCookie() }} is run every time the digest loop is run which is A LOT. Every time the page changes, click, events, etc. I'm not sure why you have this expression on the view itself but you'll need to put it elsewhere

Sign up to request clarification or add additional context in comments.

5 Comments

If OP wants to run this when ProductsController gets instantiated, should put that in the body of the controller definition.
Yes, that's a much better place for it to be. It will only be run when the controller gets instantiated
You should also remove this function call from the view and bind to a value that gets set in the controller: <img ng-src="{{products.getImageTag(product)}}">
didn't know about the digest loop before; I should have mentioned "newbie in AngularJS". Thanks
what better way to learn about it than to have it make your app explode :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.