Подтвердить что ты не робот

Как передать параметр, когда я перенаправляю страницу в angularjs с помощью ui router?

Я пытаюсь передать параметры через ui-router state.go

Однако я не уверен, как передать параметры. Вот мои коды

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

//my first.html
app.controller.('firstCtrl' ,["$scope", "$state", function($scope, $state){
    $scope.userInput <- come from user
    $scope.clickThis=function() {
        $state.go("second", $scope.userInput);
    }

}]);

//my second.html
app.controller.('secondCtrl,["$scope", "$state", function($scope, $state){
    //How do I get the parameter that is passed to here..
})

Я могу перенаправить страницу на second.html, но я не могу получить параметр, который передается моему secondCtrl. Кто-нибудь может мне помочь?

Спасибо.

4b9b3361

Ответ 1

Сначала вам нужно добавить параметр в маршрут.

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

Теперь добавьте первый контроллер

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

}]);

Во втором контроллере введите $stateParams

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

Ответ 2

Вы можете сделать это в первом контроллере: -

$state.go("second", {'input' : $scope.userInput});

Во втором контроллере введите $stateParams сервис.

app.controller('secondCtrl',["$scope", "$stateParams", function($scope, $stateParams){
    var data = $stateParams.input;
}]);

и зарегистрируйте это в своем состоянии:

  .state('second', {
        url: '/second/:input',
        templateUrl: 'second.html'
    })

Ответ 3

Вместо добавления дополнительного параметра в URL-адрес вы можете сделать это с другой стороны.

.state('second', {
    url: '/second',
    templateUrl: 'second.html',
    params: {input:null}

})

Все остальные изменения будут одинаковыми с другими ответами.