2014-02-05 44 views
0

我有一個帶有主題列表的角度CRUD應用程序。我可以添加,刪除和編輯主題。我的問題是,當我添加或刪除主題時,頁面不刷新主題的新列表。我試過,包括$route.reload();,但它給了我一個控制檯錯誤:添加/刪除數據時角頁面不刷新

的ReferenceError:$路徑沒有定義

誰能告訴我如何解決這個錯誤,或刷新頁面的更好的辦法?

HTML:

<h3>Add a Subject</h3> 

<div ng-controller="SubjectNewCtrl"> 
    <div class = "input-group"> 
<form name="newSubjectForm"> 
Name: <br /> <input type="text" name="name" ng-model="subjects.name"> 
<br /> 

<a href="#/subjects"><button type="button" class="btn btn-default"><span class="glyphicon glyphicon-arrow-left"></button></a> 
<a ng-click="createNewSubject()" class="btn btn-small btn-primary">Save Changes</a> 

</form> 
</div> 

</div> 

SubjectNewCtrl:

angular.module('myApp.controllers') 
    .controller('SubjectNewCtrl', ['$scope', 'SubjectsFactory', '$location', 
    function ($scope, SubjectsFactory, $location) { 

     // callback for ng-click 'createNewSubject': 

     $scope.createNewSubject = function() { 
      SubjectsFactory.create($scope.subjects); 
      $location.path('/subjects'); 

//This gives the console error 
      $route.reload(); 
     } 

    $scope.subjects = SubjectsFactory.query(); 

    }]); 

編輯 - 我在app.js

'use strict' 


angular.module('myApp', 
[ 
'ngRoute', 
'ngResource', 
'myApp.services', 
'myApp.directives', 
'myApp.controllers', 
]); 

angular.module('myApp') 
.config(['$routeProvider', function($routeProvider) { 
$routeProvider. 
    when('/subjects', {templateUrl: 'partials/subjects/subject-list.html', controller: 'SubjectCtrl'}). 
    when('/subjects/new', {templateUrl: 'partials/subjects/subject-new.html', controller: 'SubjectNewCtrl'}). 
    when('/subjects/:subjectid', {templateUrl: 'partials/subjects/subject-detail.html', controller: 'SubjectEditCtrl'}). 
otherwise({redirectTo: '/home'}); 
}]); 

回答

4
angular.module('myApp.controllers') 
.controller('SubjectNewCtrl', ['$scope', 'SubjectsFactory', '$location', '$route', 
function ($scope, SubjectsFactory, $location, $route) { 

    // callback for ng-click 'createNewSubject': 

    $scope.createNewSubject = function() { 
     SubjectsFactory.create($scope.subjects); 
     $location.path('/subjects'); 

//This gives the console error 
     $route.reload(); 
    } 

$scope.subjects = SubjectsFactory.query(); 

}]); 

我加上「$路線」給你的函數

+0

謝謝,這重新加載正確的網址,但呈現一個空白頁? – bookthief

+0

嗯...可能是你嘗試window.location =「YOUR_URL」? – KoIIIeY

+0

$路線。reload()創建新的作用域,你有空白頁 – KoIIIeY

0

使用scope.$apply更新的主題並不強制整個列表時,路由頁面重新加載...

$scope.createNewSubject = function() { 
    $scope.$apply(function() { 
     SubjectsFactory.create($scope.subjects); 
    }); 
}; 

btw。 $location.path('/subjects')沒有做任何事情,如果你已經在該網頁

+0

當我包括如上面我的功能停止工作.. – bookthief

+0

我不知道是什麼SubjectsFactory.create($scope.subjects);所以也許你需要刷新範圍變量方法完成後 – doodeec

+0

你有沒有考慮將ngRoute模塊包含在單獨的文件中? '