gpt4 book ai didi

javascript - AngularJS - 使用 $http 更新数组内的嵌套对象属性

转载 作者:行者123 更新时间:2023-12-03 07:40:52 25 4
gpt4 key购买 nike

我有一个包含对象数组的 JSON 文件。像这样

[
{ "id": 1, "properties": { "name": "Sam", "color": "blue" } },
{ "id": 2, "properties": { "name": "George", "color": "green" } }
];

单击按钮后,我需要更新此文件。我需要更改数组内第一个对象的属性属性内的名称属性。

<button ng-click="updateProperties()">Update Names</button>

我知道解决方案涉及$http

我在想也许可以将 $http-post 方法嵌套在 $http-get 方法中?

$scope.updateProperties = function() {
$http.get('data.json')
.then(function(response) {
var name = response.data[0].properties.name;
$http.post('data.json') {
.then(function(response) {
response.data[0].properties.name = 'Lucas';
});
}
});

祝你好运!这是一项艰难的任务。

最佳答案

您可能应该采用函数参数。另外,为什么您要在发布后尝试修改它(大概是该帖子更新了服务器)。检索、修改,然后发回服务器。此外,您还应该使用 $http.post() 的参数来发送数据。

angular $http docs

$scope.updateProperties = function() { // Update what with what???  Use one or more arguments?
return $http.get('data.json') // You could take the URL as argument
.then(function(response) {
response.data[0].properties.name = 'Lucas'; // Maybe use a function argument instead?
return $http.post('data.json', response.data);
})
}

关于javascript - AngularJS - 使用 $http 更新数组内的嵌套对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35421698/

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