I have encountered an issue regarding $stateParams
that I am trying to comprehend:
The code I am using to navigate through states is as follows: In my controller:
$scope.goToPath = function ( path, pid ) {
$scope.pid = pid;
console.log("Current scope pid : " + $scope.pid + ", Full path : " + $state.current.name);
$state.go(path, {projectId : pid} );
};
In the state definition:
state('projects.detail', {
url: '/detail',
params : { projectId : null},
templateUrl: 'app/pages/projects/detail/detailProject.html',
title: 'Detail of the project',
});
Then I am invoking this function using ng-init
:
$scope.getProjectById = function () {
projectFactory.getProject($stateParams.projectId)
.success(function (data) {
if(data == null){
$scope.errorMessage = "The project with id : " + pid + " does not exist";
} else {
$scope.project = data;
$scope.project.startDate = new Date(data.startDate);
$scope.project.endDate = new Date(data.endDate);
}
})
.error(function (data, status, headers, config) {
$scope.errorMessage = "Error 1: " + data.error + ' ' + status;
});
};
As evidenced here, I am passing $stateParams.projectId
which corresponds to the value defined in my state.
In my factory, I have the following code:
factory.getProject = function (projectId) {
console.log('Project Id from factory: '+ projectId + $stateParams.projectId);
return $http.get('http://localhost:8080/projectmanagement/Project/' + $stateParams.projectId)
};
However, when I execute this code, I am getting projectId = undefined
AND $stateParams.projectId = 4
. Since I am passing the projectId in the function, it should ideally have a value, right? Can someone help explain this discrepancy? Your assistance would be greatly appreciated. Thank you.