沿url angularjs发送参数

时间:2015-03-27 14:51:09

标签: angularjs spring

我正在开发项目,我的角色是服务器端程序员。客户端开发人员在设计页面时使用了Angular js。

我面临的问题是我们有一个页面,我需要将一个参数与url一起传递给服务器

<a  id="startQuiz" href="#/Quiz" >Start Quiz</a>

jquery代码是

$('#startQuiz').click(function (e) {
      e.preventDefault();
     window.location.href = '#/Quiz/' + selectedTopic; 
}

控制器代码

@RequestMapping(value="/Quiz", method = RequestMethod.GET)  
      public String Quiz(HttpServletRequest request,Model model,HttpServletResponse response,@RequestParam(value = "topic", required = false) String topic) throws Exception {
         System.out.println("select topic : "+topic);

}

我的主题为null null因为hash#符号被发送到服务器后没有任何内容,因此空值

Rounting文件是

app.config(function($routeProvider){
      $routeProvider
     .when("/Quiz", {templateUrl: "Quiz", controller: "PageCtrl"})
});

那么,我应该在路由中做出什么改变,这样我才能在Controller中获得主题的价值 有办法吗?

1 个答案:

答案 0 :(得分:0)

页面的URL不是重要的。该URL只会加载主页面模板。

用于将AJAX请求发送到后端控制器的URL很重要。

路线应定义为

$routeProvider
 .when("/Quiz/:topicId", {
      templateUrl: "Quiz", 
      controller: "PageCtrl"
 })

然后,使用PageCtrl中的$ routeParams服务,您可以获取topicId的值,并将相应的AJAX请求发送到后端。