AngularJS重定向到数据保存后查看

时间:2015-02-20 16:03:57

标签: angularjs redirect controller factory

从员工维护视图保存数据后,我正在尝试重定向到员工详细信息视图。我想在控制器的成功方法中做到这一点:

$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!

1 个答案:

答案 0 :(得分:14)

我明白了。出现为了使用$ location.path,你必须将$ locationProvider添加到你的app模块配置中:

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!';
    }
};

现在,当我在编辑页面上并单击“保存”时,它会重定向到该特定员工的详细信息视图