0

I'm facing an issue with accessing the array element in AngularJS. I have an array:

$scope.salesentry = [
    {
        sales_id:'',
        product_id: '',
        product_category_id:'',
        sale_qty:null,
        sale_amount:null
    }
];

I want to update the sales_id field value on some button click like:

$scope.saveData = function() {
    $scope.salesentry.sales_id='10';
});

I'm not able to access it in the above way. How can I do so?

3 Answers 3

3

salesentry is an array, so you need to access a specific element on it first using [0].

So your code becomes:

$scope.saveData = function() {
    $scope.salesentry[0].sales_id='10';
});
Sign up to request clarification or add additional context in comments.

Comments

0

Do you want to update each salesentry's sales_id ?

If yes you may use

angular.foreach($scope.salesentry, function(value, key){
    value.sales_id = 10;
});

1 Comment

thanks for answer i have one more simple issue, actually i am new to angularjs, the issue is $scope.saveData = function() { var data1; salesreg.save($scope.register,function(data){ data1=data.sales_id; }); alert (data1) }; i am not able to access the variable data1 outside the function "save()" even though i have declared the varaible in the top scope ,in the "savedata()" function how to access the varaible
0

You need to index the array

$scope.salesentry[0].sales_id = '10'

Also, no need for the comma at the end.

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.