1

How can i use $scope into a function

my code is and $scope.products do not work:

     $http.get('catalog/view/theme/default/template/confirm/fetch_product.php').then(function(response){ $scope.products = response.data.records; });
$scope.items = [];
var counter = 0;
$scope.loadMore = function($scope) {
    var last = $scope.products.length;
    for (var i = 0; i < 10; i++) {
        if(counter >= last) { break; }
         $scope.items.push($scope.products[counter]);
        counter += 1;
    }
};
$scope.loadMore();

1 Answer 1

2

Remove $scope function from loadMore function. It is killing existence of controller $scope & inside loadMore function $scope will be undefined.

$scope.loadMore = function($scope) { //<== remove `$scope` from function parameter

Also you can't get $scope.products object because you are calling loadMore method as soon as your controller get initialize. I think you should wait till initial collection of $http.get loads $scope.products & inside .then you should be calling $scope.loadMore(); function.

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

2 Comments

i did it my code is now : $scope.loadMore = function() { but still i cant use $scope.products in function
Yeah Thanks a lot

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.