0

So am using angular foreach as below

        $scope.section = [{
      "id": 1,
      "name": "name1",
      "value": "foo1"
    },
    {
      "id": 2,
      "name": "name 2",
      "value": "foo2"
    }]

    angular.forEach(section, function(item, key) {
        if(item.value)  {
            item.value = 'Some New value'
            }
    });

For some reason it doesnt work plus if I try to manipulate I get an error about duplicate value

3 Answers 3

3

Convert this

angular.forEach(section, function(item, key) {

To this

angular.forEach($scope.section, function(item, key) {

DEMO

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

Comments

2

This work for me, i just add scope.section on the forEach

 $scope.section = [{
      "id": 1,
      "name": "name1",
      "value": "foo1"
    },
    {
      "id": 2,
      "name": "name 2",
      "value": "foo2"
    }]
angular.forEach($scope.section, function(item, key) {
    if(item.value)  {
        item.value = 'Some New value';
        console.log('new value');
        }
});

Comments

1

You are forgot add $scope in object

var jimApp = angular.module("mainApp",  []);

jimApp.controller('mainCtrl', function($scope){
  
  $scope.section = [{ "id": 1, "name": "name1", "value": "foo1" },
                    { "id": 2, "name": "name 2", "value": "foo2" }];

    angular.forEach($scope.section, function(item, key) {
        if(item.value)  {
            item.value = 'Some New value'
        }
    });

  
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="mainApp" ng-controller="mainCtrl">
  {{section}}
</div>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.