简体   繁体   English

angularJS 在控制器中设置路由参数

[英]angularJS set route params in controller

I have an app which creates several surveys with random survey ids.我有一个应用程序,它使用随机调查 ID 创建多个调查。 As the ids are generated in the backend they are set in the controller.由于 id 是在后端生成的,因此它们是在控制器中设置的。 I read the documentation on that, however I do not really understand how to set the routeparams in order to always reach the page /survey/:surveryID.我阅读了相关文档,但是我不太明白如何设置 routeparams 以始终访问页面 /survey/:surveryID。

Here is my code so far:到目前为止,这是我的代码:

App Config:应用配置:

...
 .when('/survey/:surveyId', {
        templateUrl: 'views/survey.html',
        controller: 'SurveyCtrl',
        controllerAs: 'survey'
      })

Controller:控制器:

function SurveyCtrl($scope, RequestService, _, $location, $routeParams) {

  $scope.go = function () {
    $location.path('/#/survey/' + Math.random());
  };
}

View with the link to /survey/:surveyId:使用 /survey/:surveyId 链接查看:

  <div>
    <md-button ng-click="go()">Enter Survey</md-button>
  </div>

I know that this is not the right way and it is not even working.我知道这不是正确的方法,它甚至不起作用。 Can someone tell me how to dynamically create these params and set them in the controller so I can access the link with a button and then when clicked reach the survey/:surveyId page?有人能告诉我如何动态创建这些参数并将它们设置在控制器中,以便我可以使用按钮访问链接,然后在单击时到达调查/:surveyId 页面吗?

To get your work done, 为了完成你的工作,

$location.path('/survey/' + Math.random());

You can use search method to pass params as well, 您也可以使用搜索方法传递参数,

$location.path('/myURL/').search({param: 'value'});

$location methods are chainable. $ location方法是可链接的。

this produce : 这产生:

/myURL/?param=value

You could also use the updateParams method for that:您还可以使用updateParams方法:

$route.updateParams({'surveryID': Math.random()});

And access the data using $routeParams.surveryID并使用$routeParams.surveryID访问数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM