如何创建数组并推送对象属性?

时间:2016-03-08 17:08:44

标签: javascript html angularjs

我的情况是后端dataItem有值。现在我想将这些值推送到数组并设置对象属性,因此下一步是将对象属性映射到表单字段。使用下面的代码,我得到了值并将其分配给对象selectedOwners及其工作正常,但问题是每次用户addProcessOwner为每个用户创建新对象,而$scope.processDTO.prcsOwner = selectedOwners.fullName;只执行此操作将最后一个值分配给表单字段。

用户选择多个所有者后,如何在表单字段中显示所有所有者?

main.html中

<input type="text" class="form-control  customReadOnly"
            id="prcsOwner" required ng-model="processDTO.prcsOwner"
            ng-click="openPrcsOwner()" ng-disabled="PROCESS_EDIT"/>

ctrl.js

var selectedOwners = {};
$scope.selectedOwnerGrid = rcsaAssessmentService.selectedProcessOwners();
$scope.addProcessOwner = function(dataItem) {
    selectedOwners = {
        fullName: dataItem.fullName,
        workerKey: dataItem.workerKey
    }
    console.log('WORKER DATA', selectedOwners);
}
$scope.selectedProcessOwner = function() {
    $scope.prcsOwnerModal.close();
    $scope.processDTO.processOwnerWorkerKey = selectedOwners.workerKey;
    $scope.processDTO.prcsOwner = selectedOwners.fullName;
    console.log('FORM DATA', $scope.processDTO.processOwnerWorkerKey, $scope.processDTO.prcsOwner);
};

1 个答案:

答案 0 :(得分:0)

我认为你要做的是:

var selectedOwners = [];

...

var selectedOwner = {
    fullName: dataItem.fullName,
    workerKey: dataItem.workerKey
}

selectedOwners.push(selectedOwner);