控制器执行完任务后,处理路由的首选方法是什么?在我的特定场景中,我有一条路线转到“创建新记录”页面。在单击提交按钮时,控制器会执行其操作。我知道直接从控制器内部进行$location.path()
调用是不好的做法,那么告诉我的视图去新位置的首选方式是什么?
查看:
<section ng-controller="AssignmentController">
<h3>New Assignment</h1>
<form name="assignments_new">
<label for="description">Description</label> <input name="description" id="description" ng-model="assignment.description" type="text" ng-maxlength="100" placeholder="Short description of project" />
<button class="button button-mini button-primary" ng-click="createAssignment()">Create</button>
<button class="button button-mini" cancel>Cancel</button>
</form>
</section>
控制器:
$app.controller('AssignmentController', function($scope, $routeParams, $http) {
$scope.assignment = {
description: null
}
$scope.createAssignment = function() {
$http.post('/hooks/assignments_new.php', $scope.assignment).success(function(data) {
if (data.success) {
// Tell the view to redirect back to /assignments
} else {
}
}).error(function(data)) {
}
}
});
我有多年的javascript经验,但这是我第一次使用Angular。谢谢你的帮助!