【问题标题】:AngularJS redirect to view after data saveAngularJS在数据保存后重定向到视图
【发布时间】: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


    【解决方案1】:

    我想通了。看来要使用 $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!';
        }
    };
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-16
      • 2018-09-13
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多