我的角应用程序经常出现问题,在数据添加,编辑或删除后,它不会刷新页面。因此,如果我将新项目添加到主题列表中,则除非我离开页面然后再返回,否则新项目不会出现在列表中。我试过使用route.reload,然后重新设置下面的主题列表范围。我提醒一下,看看它是否被解雇 - 但是在页面重定向回主题列表之前就会出现警报,这很奇怪,因为$ location.path('/ subjects')是前两行。这里是我的控制器:添加或删除数据后,角页不刷新
angular.module('myApp.controllers')
.controller('SubjectEditCtrl', ['$scope', '$routeParams', 'SubjectFactory', 'SubjectsFactory', '$location', '$route',
function ($scope, $routeParams, SubjectFactory, SubjectsFactory, $location, $route) {
// callback for ng-click 'updateSubject':
$scope.updateSubject = function() {
//Performs an update to the server
SubjectFactory.update($scope.subject);
//Redirects to list of all subjects
$location.path('/subjects/');
//Should reset the scope of the subject list
$scope.subjects = SubjectsFactory.query();
//Should reload the page
$route.reload();
//For debugging- the alert appears BEFORE the redirect to list of all subjects happens
alert('route reload happening');
};
SubjectFactory.show({id: $routeParams.subjectId}).$promise.then(function(subject) {
$scope.subject = subject;
}, function(err) {
console.error(err);
});
}]);
任何人都可以提出一个解决办法?
编辑:主题服务
var app = angular.module('myApp.services');
app.factory('SubjectsFactory', function ($resource) {
return $resource('https://myapiurl.com/subjects', {}, {
query: { method: 'GET', isArray: true },
create: { method: 'POST' }
})
});
app.factory('SubjectFactory', function ($resource) {
return $resource('https://myapiurl.com/subjects/:id', {}, {
show: { method: 'GET', isArray: false },
update: { method: 'PATCH', params: {id: '@id'} },
delete: { method: 'DELETE', params: {id: '@id'} }
})
});
你能过去SubjectFactory呢? –
当然,请参阅编辑。 – bookthief
顺便提一下,你发送的主题是什么格式?请参阅http://docs.angularjs.org/api/ng.$http JSON漏洞保护 –