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

标签 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

最佳答案

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

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/

相关文章:

html - Bootstrap 选项卡的自定义样式

javascript - 我怎样才能过33岁!在浏览器中使用Javascript?

javascript - 如何将 ArrayBuffer 从 JS 传递到 AssemblyScript/Wasm?

html - CSS:如何增加两个元素之间的距离?

angularjs - Angular 网址正在添加不需要的字符

javascript - $http超时是什么意思?

html - @Html.ActionLink 和 Angularjs 值?

javascript - 从底部 css 动画中提升(显示)文本

javascript - 在 Angular UI Bootstrap 弹出窗口中使用表单?

html - 为什么我的代码可以在 codepen 上运行,但不能在我的编辑器上运行?