Ajax调用后如何刷新我的范围 [英] how can refresh my scope after ajax call
问题描述
我是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已经在进行中" .
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屋!