gpt4 book ai didi

javascript - 保存AngularJS后如何在页面上显示新数据

转载 作者:行者123 更新时间:2023-12-03 01:16:57 28 4
gpt4 key购买 nike

我正在将一些名称放入表单中,并尝试在保存后按字母顺序显示它们。这是包含所有保存数据的方法。 response.data 应该包含所有保存的数据:

function refreshNames() {
NameService.getNames().then(function(response) {
$scope.names = _.sortBy(response.data, "bank_name");
console.log($scope.names);
});
}
refreshNames(); //this method shows already saved names

在 saveButton 函数中,我调用上述函数并期望看到新保存的名称和旧名称。但它没有显示在列表中。

  $scope.saveButton = function() {
var name_id = _.pluck($scope.newNames, "NameID");
ClipMetadataService.saveNames(station_id, $scope.newTemplateFields).then(
function(response) {
NameService.getNames().then(function(response) {
// $scope.names = _.sortBy($scope.names.concat(response.data),
"bank_name");// this shows the newly added name in the list, but I am trying to use the refreshNames() method other than doing this
console.log(response.data);//has the newly added name
refreshNames();// this method DOES NOT show the newly added name in the list
});
};

$scope.names = _.sortBy($scope.names.concat(response.data), "bank_name"); 放入 saveButton 方法中会显示列表中新添加的名称,但我尝试使用 refreshNames() 方法来执行此操作。如何修改方法以添加新添加的 response.data

最佳答案

无需在两个方法中进行调用,将功能分开。检查下面的代码:-

function refreshNames(data) {
$scope.names = _.sortBy(response.data, "bank_name");
}

$scope.saveButton = function() {
NameService.getNames().then(function(response) {
refreshNames(response.data);// pass the response to the refreshNames method to sort them
});
};
refreshNames();

关于javascript - 保存AngularJS后如何在页面上显示新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51967638/

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