从 routeprovider 调用函数(不使用 resolve)
Call function from routeprovider (not using resolve)
我是 angular 的新手,正忙于 $routeProvider。我想在控制器中调用一个特定的函数并提供 $routeParams(我找到了一个解决方法,但它不是我们想要的。它涉及创建一个新的控制器)。
https://docs.angularjs.org/api/ngRoute/provider/$routeProvider指的是resolve(但我不想要$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);
}
});
});
有没有办法这样称呼它?
为什么不能在employeeController
中检查$routeParams
中的employeeId
是否存在并调用$scope.editEmployee
方法?
if($routeParams.employeeId){
$scope.editEmployee();
}
我是 angular 的新手,正忙于 $routeProvider。我想在控制器中调用一个特定的函数并提供 $routeParams(我找到了一个解决方法,但它不是我们想要的。它涉及创建一个新的控制器)。
https://docs.angularjs.org/api/ngRoute/provider/$routeProvider指的是resolve(但我不想要$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);
}
});
});
有没有办法这样称呼它?
为什么不能在employeeController
中检查$routeParams
中的employeeId
是否存在并调用$scope.editEmployee
方法?
if($routeParams.employeeId){
$scope.editEmployee();
}