0

I have an array of objects which is created on a button click. Now I have a scope variable that is assigned a default value.

This scope variable is part of textbox so now when user updates the textbox value that value is not getting automatically updated in all the records in bonus property of $scope.job.referralsrecords.

Code:

$scope.job.defaultBonus = 65;
function addNewReferrals{
  $scope.job.referrals.push({
                           id: null,
                           bonus: $scope.job.defaultBonus
                        });
}

Can anyone tell me why is this happening and how do I update all bonus property in $scope.job.referrals when the $scope.job.defaultBonus gets change?

2
  • 1
    do you want to update all records in $scope.job.referrals and set each one to have a bonus property that is $scope.job.defaultBonus? Commented Jun 10, 2021 at 22:29
  • @Kinglish Yes, thats right Commented Jun 10, 2021 at 22:34

1 Answer 1

1

Listening for a change from an input field. You need ng-model and ng-change. ng-model binds the input value to the scope object.

<input ng-model='job.defaultBonus' ng-change='addNewReferrals()' />

You can iterate the array with map() and use the {...} spread operator to update the record.

function addNewReferrals() {
      $scope.job.referrals = $scope.job.referrals.map(el => ({ ...el,
        bonus: $scope.job.defaultBonus
      }))
    }

I set up an object called $scope for this snippet to demonstrate, but you would just use the angular property $scope.

const $scope = {
  job: {
    defaultBonus: 0,
    referrals: [{
        id: 1,
        name: "John"
      },
      {
        id: 2,
        name: "Fred"
      },
      {
        id: 3,
        name: "Mary"
      }
    ]
  }
}

$scope.job.defaultBonus = 65;

function addNewReferrals() {
  $scope.job.referrals = $scope.job.referrals.map(el => ({ ...el,
    bonus: $scope.job.defaultBonus
  }))
}

addNewReferrals();

console.log($scope);

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

8 Comments

But the question is how do I know when the value of $scope.job.defaultBonus is changed? that's the issue. When do I call the code to update the $scope.job.referrals for the newly changed $scope.job.defaultBonus?
So I have to call the ng-change event purposely to update the $scope.job.referrals. I was sort of avoiding it because I think angularjs scope variable changes should automatically updates the value but I guess it doesn't work for Array of objects.
Well, it does automatically update the $scope.job.defaultBonus value. That is automatic by virtue of ng-model. I thought you ALSO wanted to update the referrals array items with the value, in which case the explicit ng-change function is there.
If there's no calculation involved, then instead of updating the $scope.job.referrals array item by item, you could just always reference $scope.job.defaultBonus ... I wasn't sure what your use case was
I have to update the referrals because that array is passed to an API which then updates the value in database. What do you think about using $watch in this case?
|

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.