irfan shafi
irfan shafi

Reputation: 524

How to access a predefined array in AngularJS

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?

Upvotes: 0

Views: 123

Answers (3)

Yasser Shaikh
Yasser Shaikh

Reputation: 47774

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;
});

Upvotes: 0

Mark Wade
Mark Wade

Reputation: 527

You need to index the array

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

Also, no need for the comma at the end.

Upvotes: 0

GregL
GregL

Reputation: 38103

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';
});

Upvotes: 3

Related Questions