Jose the hose
Jose the hose

Reputation: 1895

Cannot read property 'push' of undefined from Angular push

I'm trying to push attributes to a json file and I'm getting an error...

TypeError: Cannot read property 'push' of undefined

This is my controller....

'use strict';
(function () {

var userQuoteBuild = angular.module('priceQuoteApp');

userQuoteBuild.controller('quoteBuilderController', function ($scope, $http, $timeout, productsServices, userQuoteBuild) {

    $scope.getProductDetails = function (item) {

        userQuoteBuild.setProductName(item.product_name)

        userQuoteBuild.SelectedProductattributes1.push({
                bearerBandwidth: '100',
                description: 'item2'
            });

    };      

});

userQuoteBuild.controller('productDisplayer', function ($scope, userQuoteBuild) {
    $scope.userQuoteBuild = userQuoteBuild;
    $scope.$watch(function () { return userQuoteBuild.getProductName(); }, function (newValue) {
        if (newValue) $scope.selected_product_name = newValue;
    });
});

}());

and this is where I am keeping the json....

var userQuoteBuild = angular.module('priceQuoteApp');

userQuoteBuild.factory('userQuoteBuild', function () {

var SelectedProductattributes1 = [{
    bearerBandwidth: '',
    description: ''
}];
});

Can anyone see what I am doing wrong? Thanks

Upvotes: 0

Views: 383

Answers (1)

David Parlevliet
David Parlevliet

Reputation: 502

Edit: I just noticed that your app variable is the same as your factory variable. You should fix that too.

eg var app = angular.module('priceQuoteApp'); then app.factory( ....

Your factory needs to return the variable to be able to access the contents

app.factory('userQuoteBuild', function () {
    return [{
        bearerBandwidth: '',
        description: ''
    }];
});

Then to append simply

userQuoteBuild.push({ ... });

or, if you want to give yourself some more room in your factory

app.factory('userQuoteBuild', function () {
    return { 
        SelectedProductattributes1: [{
            bearerBandwidth: '',
            description: ''
        }]
    };
});

then

userQuoteBuild.SelectedProductattributes1.push({ ... });

Upvotes: 1

Related Questions