gpt4 book ai didi

javascript - 用 Angular 更新 json 值

转载 作者:行者123 更新时间:2023-11-29 18:09:56 24 4
gpt4 key购买 nike

我正在尝试使用 angular 从文本框中更新 json 对象值,但我不确定最好的方法是什么。

这是 json 对象...

   $scope.productAttributes = {
"CostRequirements":[
{
"OriginPostcode": 'NW1BT',
"BearerSize":100

}
]
}

当用户在文本字段中键入并单击按钮时,我想获取该文本字段值并将其传递到 json 对象中以替换 postcose 值 (OriginPostcode) 我试图传递一个范围变量但是没用。

 <input type="text" placeholder="Please enter postcode" class="form-control" ng-model="sitePostcode"/>

这是当用户单击按钮提交 json 时触发的函数

var loadPrices = function () {

productsServices.getPrices1($scope.productAttributes)
.then(function (res) {
$scope.selectedProductPrices = res.data.Products;
// $scope.selectedProductAddOns = res.data.product_addons;
})
.finally(function () {
$scope.loadingPrices = false;
$scope.loadedPrices = true;
});
};

谁能告诉我需要做什么才能将文本框中的用户输入放入 json 对象中?

非常感谢

最佳答案

我们没有看到的是使用按钮运行更新的函数。它应该看起来像这样

// your HTML button
<button ng-click='updateThingy()'>Update</button>
// your HTML input
<input type="text" ng-model="myObject.sitePostcode"/>


// your controller
$scope.myObject = { // ties to the ng-model, you want to tie to a property of an object rather than just a scope property
sitePostcode : $scope.productAttributes.CostRequirements[0].OriginPostcode // load in post code from productAttributes
};

$scope.updateThingy = function(){
$scope.productAttributes.CostRequirements[0].OriginPostcode = $scope.myObject.sitePostcode;
};

这是一个演示插件,用于在单击按钮时更新值,希望对您有所帮助。

http://plnkr.co/edit/8PsVgWbr2hMvgx8xEMR1?p=preview

关于javascript - 用 Angular 更新 json 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28370313/

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