Reputation:
I'm new to angular and busy with $routeProvider. I want to call a specific function in a controller and give $routeParams (i've found a workaround, but it's not desired. It involves creating a new controller).
https://docs.angularjs.org/api/ngRoute/provider/$routeProvider refers to resolve (but i don't want the function inside the $routeProvider).
This is the problem:
(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<<----------------------
})
});
})();
This is the controller:
(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);
}
});
});
Is there a way to call it like this?
Upvotes: 0
Views: 882
Reputation: 3385
Why can't you check the existence of employeeId
in $routeParams
inside employeeController
and call $scope.editEmployee
method?
if($routeParams.employeeId){
$scope.editEmployee();
}
Upvotes: 1