0

我是 Angular 新手,忙于 $routeProvider。我想调用控制器中的特定函数并提供 $routeParams (我找到了一种解决方法,但它不是我们所希望的。它涉及创建一个新控制器)。

https://docs.angularjs.org/api/ngRoute/provider/$routeProvider指的是解析(但我不希望 $routeProvider 中的函数)。

这就是问题:

(function () {
angular
    .module('app', ['ngRoute', 'employee'])
    .config(function ($routeProvider) {
        $routeProvider.when('/settings/employees',
            {
                templateUrl: 'app/Employee/employee-table.html',
                controller: 'employeeController'
            })
        $routeProvider.when('/settings/employees/add/form',
            {
                templateUrl: 'app/Employee/employee-add-form.html',
                controller: 'employeeController'
            })
        $routeProvider.when('/settings/employees/edit/:employeeId/form',
            {
                templateUrl: 'app/Employee/employee-edit-form.html',
                controller: 'employeeController',
                //Call to 'editEmployee' method WITH $routeParams<<----------------------
            })
    });
})();

这是控制器:

(function () {
'use strict';

angular
    .module('employee')
    .controller('employeeController', function ($scope, $location, $routeParams, employeeFactory) {
        // Get all employees
        $scope.getAllEmployees = function () {
            return employeeFactory.getAllEmployees();
        }

        $scope.getEmployeeById = function (employeeId) {
            //TODO: IMPLEMENT:      NOT YET IMPLEMENTED!
        }

        // Add employee
        $scope.addEmployee = function (addEmployeeForm) {
            if (addEmployeeForm.$valid) {
                employeeFactory.addEmployee($scope.employee);
            }
        }

        // THIS IS THE FUNCTION TO CALL<<---------------------------------------------
        $scope.editEmployee = function () {
            console.log('reached');
            if ($routeParams) {
                console.log($routeParams);
            }
        }

        // Delete given employee
        $scope.deleteEmployee = function (employee) {
            employeeFactory.deleteEmployee(employee);
        }

        $scope.cancelForm = function () {
            $scope.go('/settings/employees');
        }

        // Reset the form
        $scope.resetForm = function () {
            $scope.employee = {}
        }

        // Route to path
        $scope.go = function (path) {
            console.log(path);
            $location.path(path);
        }
    });
});

有没有办法这样称呼它?

4

1 回答 1

1

为什么不能检查employeeIdin $routeParamsinsideemployeeController和 call$scope.editEmployee方法的存在?

if($routeParams.employeeId){
  $scope.editEmployee();
}
于 2016-04-03T17:23:59.687 回答