Play Framework 2.1 - AngularJS routing - best solution?

前端 未结 7 1900
既然无缘
既然无缘 2020-12-23 16:57

I am working my way through the AngularJS tutorial. Angular uses it\'s own JS routing mechanism to allow for single page apps. A sample routing file for Angular looks like t

7条回答
  •  醉酒成梦
    2020-12-23 17:36

    For question #1 you could introduce a route like this:

    /partials/:view    controllers.Application.showView(view:String)
    

    Then in your controller you would need to map from view name to actual view:

    Map("phone_index" -> views.html.partials.phone_index())
    

    You might want to render the templates lazy or require the request to be present, then you should probably do something like this:

    val routes = Map(
      "phone_index" -> { implicit r:RequestHeader => 
         views.html.partials.phone_index()) 
      }
    

    Your action would look something like this:

    def showView(view:String) = 
      Action { implicit r =>
        routes(view)
      }
    

    If you want a specific controller method for a certain route (question #2) you simple add a route above the dynamic one:

    /partials/specific    controllers.Application.specific()
    

提交回复
热议问题