Kishan
Kishan

Reputation: 388

$stateParams is undefined in AngularJS

In my project am using UI-router. Please check my below code to fix it

index.js

.state('survey.surveyList', {
        url: '/:id',
        templateUrl: 'survey/surveyList.html',
        controller: 'surveyController'
    });

surveyController.js

angular.module("adminsuite").controller("surveyController",['getAllSurveyService','$http','AuthenticationService', '$scope','$rootScope', '$state', '$stateParams', function(getAllSurveyService,$http, AuthenticationService,$scope,$rootScope,$state,$stateParams){
       $scope.getProjSurveys = function(projId){
        var i;
        for(i in $scope.allProjects){
         if(projId == $scope.allProjects[i].ProjectID){
          $scope.allSurveys = $scope.allProjects[i].Surveys;
         }
       }
         angular.element(document.querySelectorAll(".surveymodule")).removeClass('toggled');
      console.log($state.params.id);
  };
}]);

index.html

<li ng-repeat="obj in allProjects track by  $id(obj)">
                <a ui-sref="survey.surveyList({id: obj.ProjectID})" ng-click="getProjSurveys(obj.ProjectID)" data-id="{{obj.ProjectID}}">{{obj.ProjectName}}<span class="projectsetting"><img src="./images/settings.png"/></span></a>
            </li>

Above are my codes. when i try to access the id value using $stateParams it shows as undefined. Using this i am trying to call another function by passing $stateParams.id as parameter but it is not working. Please help

Upvotes: 1

Views: 1534

Answers (1)

Rakesh Chand
Rakesh Chand

Reputation: 3113

Assuming you're passing id like following :-

$state.go('survey.surveyList', {id : id});

You can get the same id using

$state.params.id

Please tell me if you have anything different than this?

EDIT Prefer passing this way

<li ng-repeat="obj in allProjects track by  $id(obj)">
    <a ng-click="getProjSurveys(obj.ProjectID)" data-id="{{obj.ProjectID}}">{{obj.ProjectName}}<span class="projectsetting"><img src="./images/settings.png"/></span></a>
</li>

function getProjSurveys(ProjectID){
    $state.go('survey.surveyList', {id : ProjectID});
}

Upvotes: 2

Related Questions