gpt4 book ai didi

javascript - AngularJS/Javascript - 如何用 JSON 替换整个对象

转载 作者:行者123 更新时间:2023-12-03 09:40:01 26 4
gpt4 key购买 nike

我正在尝试使用 JSON 回调更新 objectValue
根据下面的代码,如果我离开 objectValue.data 它只会更新该对中的值。我尝试关闭 .data 但没有任何响应。

有没有办法可以用我收到的 JSON 请求完全覆盖 objectValue

app.service('sharedProperties', function() {
var objectValue = {
'data': 'Click to change'
};
return {
setObject: function(value) {
objectValue.data = value;
},
getObject: function() {
return objectValue;
}
};
});

干杯!

最佳答案

我不认为这有什么问题。我尝试过:

....    
// service
setObject: function(value) {
objectValue = value;
},


// and in the controller
app.controller('myController', function($scope, sharedProperties) {
$scope.stringValue = sharedProperties.getString;
$scope.objectValue = sharedProperties.getObject();
$scope.setString = function(newValue) {
$scope.objectValue.data = newValue;
sharedProperties.setObject(newValue);
alert(sharedProperties.getObject());
};
});

它会提醒该对象。也许您的代码的另一部分有问题。尝试一步步记录/警报,看看会发生什么。

关于javascript - AngularJS/Javascript - 如何用 JSON 替换整个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31214262/

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