0

私は $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