我有一個帶有主題列表的角度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'});
}]);
謝謝,這重新加載正確的網址,但呈現一個空白頁? – bookthief
嗯...可能是你嘗試window.location =「YOUR_URL」? – KoIIIeY
$路線。reload()創建新的作用域,你有空白頁 – KoIIIeY