ajax调用后如何刷新我的范围 [英] how can refresh my scope after ajax call

查看:29
本文介绍了ajax调用后如何刷新我的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 angularjs 的新手.我有人员列表,每个人都有编辑和删除按钮.当我单击编辑按钮 ng-dialog 框已打开并显示人员详细信息时,人员可以更改和保存数据库上的信息,在保存按钮 ajax 调用触发器和更新数据库上的信息之后.

I am newbie for angularjs.I have list of persons and each person have edit and delete button. when i click to edit button ng-dialog box was open and show person details and person can change and save information on database,behind save button ajax call trigger and update information on database.

更新有关数据库的信息运行良好,但在 UI 方面,我的视图并未反映我的数据库更改.我曾尝试应用 "$scope.$apply();" 方法,但我收到错误消息 "$digest already in progress".

Updating information on database work well but on UI side my view doesn't reflect my database changes. I had tried to apply "$scope.$apply();" method but i got error message "$digest already in progress".

请帮助我,如何在 ajax 调用后刷新我的范围.

Please help me,how can refresh my scope after ajax call.

推荐答案

你可以使用 共享服务广播 通过此服务的任何事件.可以在任何带有 $scope.$on 的控制器中收听广播事件.

You can use shared service for that and broadcast any event through this service. Broadcasted event can be listened in any controller with $scope.$on.

例如:

angular.module("app", []).factory("sharedService", function($rootScope){

    var mySharedService = {};

    mySharedService.values = {};

    mySharedService.personWasUpdated = function(){
        $rootScope.$broadcast('update');
    }

    return mySharedService; 
});

Ctrl 用于个人编辑.

Ctrl for person editing.

app.controller('personEditController', ['$scope', 'sharedService', '$http', function ($scope, sharedService, $http) {
   $scope.updatePerson = function(newPerson){
       $http.post("../some URL/..", {person: newPerson})
          .success(function(data){
             sharedService.personWasUpdated(); //event broadcasing
          })
   };
}

Ctrl 显示人员列表.

Ctrl for displaying list of persons.

app.controller('personController', ['$scope', 'sharedService', '$http', function ($scope, sharedService, $http) {
   var loadPersonsData = function(){
      $http.get("../some URL/..").
         .success(function(data){
            $scope.persons = data;
         })
   };
   loadPersonsData(); //first load

   $scope.$on('update', function () {
      loadPersonsData(); // load after update of any person
   });
}

这篇关于ajax调用后如何刷新我的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆