如何取回传递的参数$ state.transitionTo?

时间:2016-09-29 05:47:13

标签: angularjs

我有一个名为editDetails()的函数..我可以导航到viewproviderEdit.html页面但是如何接收我在新控制器中传递的参数

功能

          $scope.editDetails = function() {
            console.log("id is ", id)
            $state.transitionTo('app.viewproviderEdit', {stateParamKey: id});
          }

app.router.js

                .state('app.viewproviderEdit', {
                    url: '/view-provider-edit/:id',
                    templateUrl: 'manage_provider/viewproviderEdit.html',
                    controller: 'viewprovidereditCtrl',
                    resolve: {
                        deps: ['$ocLazyLoad',
                            function($ocLazyLoad) {
                                return $ocLazyLoad.load('xeditable').then(
                                    function() {
                                        return $ocLazyLoad.load('manage_provider/viewproviderEdit.js');
                                    }
                                );
                            }
                        ]
                    }
                })

viewproviderEdit.js

  app.controller('viewprovidereditCtrl', ['$scope', '$filter','$state',
    function($scope, $filter,$state){
        if($state.current.name === 'app.viewproviderEdit')
        {
            console.log("coming inside new controller");
            // above console is working
        }
   }]);

如何在viewprovidereditCtrl控制器中获取id。任何人都可以帮助我。

1 个答案:

答案 0 :(得分:1)

 $scope.editDetails = function() {
        console.log("id is ", id)
        $state.transitionTo('app.viewproviderEdit', {id:id});
      }

并更改您的控制器

 app.controller('viewprovidereditCtrl', ['$scope','$filter','$state','$stateParams',
function($scope, $filter,$state,$stateParams){
    if($state.current.name === 'app.viewproviderEdit')
    {
        console.log("coming inside new controller");
        // above console is working
        console.log($stateParams.id);
    }
}]);

使用$ stateParams:)