【发布时间】:2015-02-20 16:03:56
【问题描述】:
从员工维护视图保存数据后,我试图重定向到员工详细信息视图。我想在控制器的成功方法中这样做:
$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()
window.location.href = 'Index.html#/employee-details/' + employee.EmployeeNumber;
这有效,但它也会产生一个丑陋的运行时错误:
JavaScript runtime error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
【问题讨论】:
标签: angularjs redirect controller factory