简体   繁体   English

如何将对象数组分配给数组?

[英]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 ? 我现在有一个来自后端的json响应controlOwners ,我想将响应设置为$scope.selectedOwners = [] ,但是它在控制台中给我未定义的任何提示这是怎么了?

ctrl.js 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 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",
}],

My guess is as I stated in my comment above, that you have the incorrect casing in your reference to controlOwners. 正如我在上面的评论中所述,我的猜测是您对controlOwners的引用中包含不正确的大小写。 You have spelled it with a lower case 'o' ('controlowners'), when it should be upper case ('controlOwners'). 您应将其拼写为小写字母“ o”(“ controlowners”),而应将其拼写为大写(“ controlOwners”)。 And hence you are getting an undefined error. 因此,您将收到未定义的错误。

first make sure that this isn't a typo. 首先请确保这不是拼写错误。 You have both: 你们两个都有:

controlOwners
       ^

and

controlowners
       ^

in the JSON and code. 在JSON和代码中。


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). 然后,如果拼写错误只是您的问题而不是您的代码,并且取决于您在其他地方使用$scope.selectedOwners ,则您可能不想替换 if语句中列表的引用,而希望对其进行更新现有列表(即使您必须先清除它)。

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. 而不是可能在其他时间触发的回调中的if,则忽略它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM