Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ui路由器在angularjs中重定向页面时如何传递参数?_Javascript_Angularjs_Angular Ui - Fatal编程技术网

Javascript 使用ui路由器在angularjs中重定向页面时如何传递参数?

Javascript 使用ui路由器在angularjs中重定向页面时如何传递参数?,javascript,angularjs,angular-ui,Javascript,Angularjs,Angular Ui,我试图通过ui router state.go传递参数 但是,我不确定如何传递参数。这是我的密码 app.config(function($stateProvider) { $stateProvider .state('first', { url: '/first', templateUrl: 'first.html' }) .state('second', {

我试图通过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..
})
app.config(函数($stateProvider){
$stateProvider
.州('第一'{
url:“/first”,
templateUrl:'first.html'
})
.州('第二'{
url:“/秒”,
templateUrl:'second.html'
})
})
//我的第一个.html
应用程序控制器。('firstCtrl',[“$scope”,“$state”,函数($scope,$state){

$scope.userInput首先必须在路由中添加参数

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

}]);

您可以在第一个控制器中这样做:-

$state.go("second", {'input' : $scope.userInput});
在第二个控制器中,注入服务

并在您所在州注册:

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

您可以通过另一种方式来完成,而不是在url中添加额外的参数

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

})

所有其他更改都将与其他答案相同。

谢谢您的帮助。我给Daus908绿色,因为他表示我需要在路由中添加参数,这正是我缺少的。:D+1@FlyingCatnp.我想我在这个答案中也提到了。但是另一个答案比我的答案早了6秒:)如何使用上述方式传递对象?在上面的示例中,它在URL中显示“id”的值,显然,如果我想传递对象,我不想在URL中显示它。那怎么可能呢?谢谢你救了我的命!棒极了!老兄..一个常见的错误是,人们不保持变量名与模板URL中的变量名相同。。。
  .state('second', {
        url: '/second/:input',
        templateUrl: 'second.html'
    })
.state('second', {
    url: '/second',
    templateUrl: 'second.html',
    params: {input:null}

})