我很有兴趣使用$ 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);
}
});
});
有没有办法像这样称呼它?
答案 0 :(得分:1)
为什么无法检查employeeId
内$routeParams
内employeeController
是否存在并调用$scope.editEmployee
方法?
if($routeParams.employeeId){
$scope.editEmployee();
}