hussain
hussain

Reputation: 7083

How can i assign array of object to array?

I have a json response controlOwners from backend now i want to set response to $scope.selectedOwners = [] , but its giving me undefined in console any idea what is going wrong here ?

ctrl.js

$scope.selectedOwners = [];
if ($state.is('app.editControl')) {
          $scope.selectedOwners = angular.copy($scope.controlowners);
          console.log('EDIT CONTROL OWNERS DATA', $scope.selectedOwners);
        }

json.js

"controlOwners": [{
    "workerKey": -1093,
    "sourceFeed": null,
    "statusLookUpCode": null,
    "externalId": null,
    "createdUserText": null,
    "createdTimestamp": null,
    "modifiedUserText": null,
    "modifiedTimestamp": null,
    "stdId": "ZK84T1N",
    "ccId": null,
    "empClasId": null,
    "deptId": null,
    "fullName": "Rajasekaran, Shanmuga",
}],

Upvotes: 0

Views: 51

Answers (2)

ManoDestra
ManoDestra

Reputation: 6473

My guess is as I stated in my comment above, that you have the incorrect casing in your reference to controlOwners. You have spelled it with a lower case 'o' ('controlowners'), when it should be upper case ('controlOwners'). And hence you are getting an undefined error.

Upvotes: 1

zmo
zmo

Reputation: 24812

first make sure that this isn't a typo. You have both:

controlOwners
       ^

and

controlowners
       ^

in the JSON and code.


Then if the typo is just in your question and not in your code, and depending on how you're using $scope.selectedOwners elsewhere, you might not want to replace the reference of the list in the if statement, but rather have it update the existing list (even if you have to clear it first).

But obviously, if your code has exactly:

$scope.selectedOwners = [];
if ($state.is('app.editControl')) {

and not the if in a callback that might be triggered at another time, then ignore that.

Upvotes: 0

Related Questions