$ stateParams在AngularJS中未定义

时间:2016-09-15 09:54:30

标签: angularjs angular-ui-router

在我的项目中使用UI路由器。请检查我的以下代码以解决此问题

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>

以上是我的代码。当我尝试使用$ stateParams访问id值时,它显示为undefined。使用这个我试图通过传递$ stateParams.id作为参数调用另一个函数,但它无法正常工作。请帮忙

1 个答案:

答案 0 :(得分:2)

假设您正在传递ID如下: -

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

您可以使用

获得相同的ID
$state.params.id

请告诉我你有什么不同吗?

修改 喜欢通过这种方式

<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});
}