gpt4 book ai didi

javascript - 如何添加到 Angular 中现有 json 对象中的数组?

转载 作者:行者123 更新时间:2023-12-03 10:15:17 25 4
gpt4 key购买 nike

我的 json 对象“flowComponents”包含一个字符串(名称)和一个字符串数组(版本)。例如:

    {
"_id": "553e87f3205465e83b46999b",
"name": "FLOWCOMPONENT_CONTACTCOMBINATION_EDITION",
"__v": 0,
"edition": [
"billing",
"billingDelivery",
"default",
"deliveryAddressOnly",
"deliveryBillingLicensee",
"deliveryBillingLicenseeWithWrapper",
"deliveryLicensee",
"deliveryOnlyCopyToAll",
"licenseeDelivery",
"sassDefault",
"sassDeliveryOnlyCopyToAll"
]
}

我需要向这个现有的 flowComponents 对象添加/连接另一个版本。我的表单有一个下拉列表,其中包含现有 flowComponents 的名称,以及一个文本区域,该文本区域生成每行文本的数组:

<form ng-submit="addToExistingFlowComponent()">
<div class="interact">
<select ng-model="existingName" chosen options="flowComponents" ng-options="item as item.name for item in flowComponents" data-placeholder="Select a flow component...">
</select>
</div>

<div class="interact">
<label class="interact-label">Enter each edition on a new line.</label>
<textarea id="text_area" placeholder="Edition" ng-model="existingEditionList" ng-list="&#10;" ng-trim="false"></textarea>
</div>
<button type="submit">Submit</button>
</form>

这是我的 Controller 中的添加版本方法:

$scope.addToExistingFlowComponent = function(){
if(!$scope.existingName || $scope.existingName === '') { return; }

var existingFC = $scope.existingName._id;

sendAppData.postEdition( existingFC, {
edition: $scope.existingEditionList
});

$scope.existingName = '';
$scope.existingEditionList = '';
};

这是将数据发送到服务器的方法:

this.postEdition = function(existingFC, newEdition) {
return $http.post('/new-flow-component', newEdition).success(function(data){
flowComponents.push(data);
});
};

问题是,这是将数据推送到新对象,而不是添加到现有对象。我能够将现有对象的 _id 传递给现有的 FC 参数,但我不知道如何在函数(数据)内部获取该 _id 以便将其插入正确的版本数组中。

最佳答案

我修改了您的代码,以将文本区域中的新版本附加到您选择的版本数组中。我删除了对服务器的发布,并将其提交的"new"版本附加到版本数组中。这是此示例的 Plunker:http://plnkr.co/edit/U2BE9Sdlictj9dEIWkjc?p=preview

希望这对你有帮助

Controller :

app.controller('MainCtrl', function($scope) {

$scope.flowComponents = [{
"_id": "553e87f3205465e83b46999b",
"name": "FLOWCOMPONENT_CONTACTCOMBINATION_EDITION",
"__v": 0,
"edition": [
"billing",
"billingDelivery",
"default",
"deliveryAddressOnly",
"deliveryBillingLicensee",
"deliveryBillingLicenseeWithWrapper",
"deliveryLicensee",
"deliveryOnlyCopyToAll",
"licenseeDelivery",
"sassDefault",
"sassDeliveryOnlyCopyToAll"
]
}]

$scope.addToExistingFlowComponent = function(){
if(!$scope.existingName || $scope.existingName === '') { return; }

var existingFC = $scope.existingName._id;
var newEdition = {
edition: $scope.existingEditionList
};

console.log($scope.existingName);
console.log(newEdition);
for(var i=0;i<$scope.existingEditionList.length;i++){
$scope.existingName.edition.push($scope.existingEditionList[i]);
}

console.log($scope.flowComponents);

$scope.existingName = '';
$scope.existingEditionList = '';
};

});

您的 HTML:

<body ng-controller="MainCtrl">

<form ng-submit="addToExistingFlowComponent()">
<div class="interact">
<select ng-model="existingName" chosen options="flowComponents" ng-options="item as item.name for item in flowComponents" data-placeholder="Select a flow component...">
</select>
</div>

<div class="interact">
<label class="interact-label">Enter each edition on a new line.</label>
<textarea id="text_area" placeholder="Edition" ng-model="existingEditionList" ng-list="&#10;" ng-trim="false"></textarea>
</div>
<button type="submit">Submit</button>
</form>

</body>

关于javascript - 如何添加到 Angular 中现有 json 对象中的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29905790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com