0

I have an object for selectedProduct. I now want to create an array of these selectedProduct objects so I can build a list using ng-repeat. I;ve set the scope to an empty array but what is the angular way to push these so I can access them via ng-repeat, i.e. product in products.

 $scope.products = [];

    $scope.getProduct = function() {
        ProductService.getProduct($scope.eanInput.ean)
        .then(function(product) {
            $scope.selectedProduct = product;
            $scope.selectedProduct.ean = $scope.eanInput.ean;
            $scope.selectedProduct.qtyInput = 1;
            $scope.focusOn = 'qty';

            $scope.eanInput.productFound = true;
        })
        .catch(function() {
            $scope.eanInput.productFound = false;
        });
    };
0

2 Answers 2

5

To my knowledge there is no angular way of pushing an object into an array, you can just use the default javascript way:

$scope.products.push(product);

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

1 Comment

To whoever downvoted this: feel free to give me a reason why
2

I would just push them to the array to be honest :

 $scope.products = [];

    $scope.getProduct = function() {
        ProductService.getProduct($scope.eanInput.ean)
        .then(function(product) {
            $scope.selectedProduct = product;
            $scope.selectedProduct.ean = $scope.eanInput.ean;
            $scope.selectedProduct.qtyInput = 1;
            $scope.focusOn = 'qty';
            $scope.products.push(product)


            $scope.eanInput.productFound = true;
        })
        .catch(function() {
            $scope.eanInput.productFound = false;
        });
    };

then in html you can do :

<div ng-controller="YourCtrl">
    <h1 ng-repeat="product in products">
    {{product.ean}}
    </h1>
</div>

Comments

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.