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

时间:2018-08-22 13:07:24

标签: javascript html angularjs

我将一些名称放入表格中,并尝试在保存后按字母顺序显示它们。这是具有所有已保存数据的方法。 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的方法?

1 个答案:

答案 0 :(得分:1)

No need to make calls in both the methods, separate the functionality. Check the code below:-

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();