2014-02-11 59 views
2

我的角应用程序经常出现问题,在数据添加,编辑或删除后,它不会刷新页面。因此,如果我将新项目添加到主题列表中,则除非我离开页面然后再返回,否则新项目不会出现在列表中。我试过使用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'} } 
    }) 
}); 
+0

你能过去SubjectFactory呢? –

+0

当然,请参阅编辑。 – bookthief

+0

顺便提一下,你发送的主题是什么格式?请参阅http://docs.angularjs.org/api/ng.$http JSON漏洞保护 –

回答

0

更改请求的结构稍微固定problem-所以不是

$scope.updateSubject = function() { 
    SubjectFactory.update($scope.subject); 
    $location.path('/subjects/'); 
    $scope.subjects = SubjectsFactory.query(); 
    $route.reload(); 
}; 

现在

$scope.updateSubject = function() { 
       SubjectFactory.update($scope.subject).$promise.then(function (subject) { 
        $scope.subject = subject; 
        $location.path('/subjects/'); 
        $route.reload(); 
       }, function (err) { 
        console.error(err); 
       }); 
      }; 
4

您需要申请变更的范围,这是由下面的代码做了一些时间:

$scope.$apply(); 

但这只能如果它不是做“ $消化“阶段,否则会抛出异常。所以你需要首先检查它不在“$摘要”阶段,那么你可以应用它。下面是我使用的安全应用更改代码的例子:

safeApply: function (scope, callback) { 
    if (scope.$$phase != '$apply' && scope.$$phase != '$digest' && 
     (!scope.$root || (scope.$root.$$phase != '$apply' && scope.$root.$$phase != '$digest'))) { 
     scope.$apply(); 
    } 
    if (angular.isFunction(callback)) { 
     callback(); 
    } 
} 
+0

我包含$ scope.apply(),但它对刷新没有影响,并且没有抛出异常! – bookthief

+0

是否在这行之后添加了$ scope($ apply())? $ scope.subjects = SubjectsFactory.query(); –

+0

是的,我现在得到一个控制台错误 - TypeError:对象#有没有方法'适用' – bookthief

0

我可以建议下一方式:

你不能从数据库数据,添加后,你可以伊斯利推新添加的对象到$scope.items

例子:

$scope.add = function (newItem) { 
     DataService.addItem(newItem).then(function(){ 
      $scope.items.push(newItem); 
      //or for removing 
      //$scope.items.splice($scope.items.indexOf(newItem), 1); 
     }); 
    }; 

并调整工厂:

addItem: function (newProject) { 
     $http.post('Api/Controller/Post').then(function(successResult){ 
      ... 
     }, function (errorResult) { 
      ... 
     }); 
    } 

项目将在$scope.items后才成功调用服务器端的方法来添加。