How to pass parameter when I redirect the page in angularjs using ui router?

前端 未结 3 1693
悲哀的现实
悲哀的现实 2020-12-31 07:44

I am trying to pass parameters via the ui-router state.go

However, I am not sure how to pass the parameters. Here are my codes

app.config(function($s         


        
3条回答
  •  礼貌的吻别
    2020-12-31 08:40

    First you have to add parameter in route.

    app.config(function($stateProvider) {    
        $stateProvider
            .state('first', {
                url: '/first',
                templateUrl: 'first.html'
            })
            .state('second', {
                url: '/second/:id',
                templateUrl: 'second.html'
            })
    });
    

    Now add in first controller

    app.controller.('firstCtrl' ,["$scope", "$state", function($scope, $state){
        $scope.userInput <- come from user
        $scope.clickThis=function() {
            $state.go("second", { id: $scope.userInput });
        }
    
    }]);
    

    In second controller inject $stateParams

    //my second.html
    app.controller.('secondCtrl',["$scope", "$state", "$stateParams", function($scope, $state, $stateParams){
        $scope.id = $stateParams.id;
    })
    

提交回复
热议问题