AngularJS在数据保存后重定向到视图
从employee maintenance视图保存数据后,我尝试重定向到employee details视图。我希望在控制器的成功方法中执行此操作:AngularJS在数据保存后重定向到视图,angularjs,redirect,controller,factory,Angularjs,Redirect,Controller,Factory,从employee maintenance视图保存数据后,我尝试重定向到employee details视图。我希望在控制器的成功方法中执行此操作: $scope.insertEmployee = function (employee) { if (employee && $scope.IsNew) { employeeFactory.insertEmployee(employee) .success(function (data)
$scope.insertEmployee = function (employee) {
if (employee && $scope.IsNew) {
employeeFactory.insertEmployee(employee)
.success(function (data) {
// after a successful save, redirect the user to the details view
$location.path('/employee-details/' + employee.EmployeeNumber);
if (!$scope.$$phase)
$scope.$apply()
})
.error(function (error) {
$scope.status = 'Unable to save the employee data: ' + error;
});
}
else {
$scope.status = 'You are missing required information!';
}
};
这是我的工厂:
factory.insertEmployee = function (employee) {
url = baseAddress + "employee/insert/";
return $http.post(url, employee);
};
我的asp.net webapi控制器:
[Route("api/employee/insert/")]
public HttpResponseMessage Post(Employee employee)
{
HttpResponseMessage response = null;
// check for the employee
Employee employeeCheck = employeeService.GetEmployeeById(employee.EmployeeNumber);
if (employeeCheck == null)
{
if (ModelState.IsValid)
{
employeeService.CreateEmployee(employee);
response = Request.CreateResponse(HttpStatusCode.OK);
}
else
{
response = Request.CreateResponse(HttpStatusCode.BadRequest, "There was a problem with saving the data.");
}
}
else
{
response = Request.CreateResponse(HttpStatusCode.Conflict, "The item already exists");
}
return response;
}
这个问题似乎经常被问到,但没有一个解决方案对我有效
编辑:我使用了以下内容而不是$location.path()
这是可行的,但也会产生一个丑陋的运行时错误:
JavaScript runtime error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
我想出来了。为了使用$location.path,您必须将$locationProvider添加到您的应用程序模块配置中:
var app = angular.module('myApp', ['ngRoute']);
app.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
controller: 'HomeController',
templateUrl: 'Partials/ViewStart.html'
})
然后,在控制器中注入$location:
app.controller('EmployeeController', function ($scope, employeeFactory, $location)
最后,您可以设置路径:
$scope.insertEmployee = function (employee) {
if (employee && $scope.IsNew) {
employeeFactory.insertEmployee(employee)
.success(function () {
$scope.status = 'The item was saved!';
$location.path('/employee-details/' + employee.EmployeeNumber);
})
.error(function (error) {
$scope.status = 'Unable to save the employee data: ' + error;
});
}
else {
$scope.status = 'You are missing required information!';
}
};
现在,当我在编辑页面上单击保存时,它会重定向到该特定员工的详细信息视图
$scope.insertEmployee = function (employee) {
if (employee && $scope.IsNew) {
employeeFactory.insertEmployee(employee)
.success(function () {
$scope.status = 'The item was saved!';
$location.path('/employee-details/' + employee.EmployeeNumber);
})
.error(function (error) {
$scope.status = 'Unable to save the employee data: ' + error;
});
}
else {
$scope.status = 'You are missing required information!';
}
};