0

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

1 Answer 1

1

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({ ... });
Sign up to request clarification or add additional context in comments.

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.