播放框架2.1 - AngularJS路由 - 最好的解决办法? [英] Play Framework 2.1 - AngularJS routing - best solution?

查看:167
本文介绍了播放框架2.1 - AngularJS路由 - 最好的解决办法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过AngularJS教程工作我的方式。角使用它自己的JS路由机制来实现单页面的应用程序。对角的样本路由文件看起来像这样:

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 this:

angular.module('phonecat', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/phones', {templateUrl: '/partials/phone-list',   controller: PhoneListCtrl}).
      when('/phones/:phoneId', {templateUrl: 'partials/phone-detail', controller: PhoneDetailCtrl}).
      otherwise({redirectTo: '/phones'});
}]);

我试图想出一个好地方来存储我的谐音(角特定的HTML文件)。理想情况下,我想从游戏中模板它们的能力(即有他们为* .scala.html文件)。我可以完成使用这个戏routes文件,像这样:

I am trying to come up with a good place to store my partials (Angular specific HTML files). Ideally i WOULD like the ability to template them from within Play (i.e. have them as *.scala.html files). I can accomplish this using a a Play routes file like so:

GET     /partials/phone_index       controllers.Application.phone_index

我基本上是谐音/像这样的控制器操作:

I basically partials/ to a controller action like this:

def phone_index = Action {
  Ok(views.html.partials.phone_index())
}

我要寻找的解决方案是两个理想的组合:

The solution I am looking for is a combination of two ideals:


  1. 我想有某种映射,让我访问任何文件在/分/ *和取回部分文件。

  2. 我想压倒一切的路由到特定的部分,所以我的 CAN 使用控制器的动作与数据动态填充(罕见)。

  1. I would have some sort of mapping that lets me visit any file under /partial/* and get back the partial file.
  2. I would like the overriding a route to a specific partial so I CAN use a controller action to dynamically fill with data (rare).

任何想法?

推荐答案

当我尝试类似我来这是更好地打破它在两个部分的结论的内容:

When I was trying something similar I came to the conclusion that it's better to break it on 2 parts:


  • 使用发挥你通过Ajax交互后端调用

  • 存放角模板播放公共文件夹(类似 /公/角/ ),并使用默认AngularJs方式向模板图

  • Use Play as a backend you interact with via Ajax calls
  • Store the Angular templates in Play public folder (something like /public/angular/) and use the default AngularJs way to map the templates

我知道这听起​​来并不大,真的不回答你如何做的问题,而是试图以两种框架链接可能会有问题,由于道路模板及其网址角映射,效益将是非常小的任何变化将意味着大量的工作,从而消除播放和角度,快速发展可以说是最主要的好处。

I know it doesn't sound great and really doesn't answer your question on how to do it, but trying to link both frameworks may be problematic due to the way templates and their urls are mapped in Angular, and the benefit will be very small as any change will imply a lot of work, thus removing the arguably main benefit of both Play and Angular, rapid development.

这也可以让你更好地分离的担忧,而如果你的项目的增长,你可以只取AngularJS code远作为一个独立的应用程序连接到后端,然后它会正常工作可能很重要。

This also allows you to separate concerns better, which if your project grows may be important as you can just take the AngularJS code away as a standalone app connecting to a backend, and it will work fine.

您可以在此 Github上库<明白我说的一些示例code(基于AngularJS的TODO教程) / A>。我警告你,在code不是太好,但应该给你一个想法,作为奖金向您展示如何茉莉花融入游戏,为AngularJS单元测试。

You can see some sample code of what I said (based on the TODO tutorial of AngularJS) in this Github repository. I warn you, the code is not too nice, but should give you an idea and as a bonus shows you how to integrate Jasmine into Play, for AngularJS unit testing.

这篇关于播放框架2.1 - AngularJS路由 - 最好的解决办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆