我正在开发一个主要基于 Symfony2 和 Twig-Templates 的项目。我现在想编写一个基于 AngularJS 的新包。也就是说,大多数网站的菜单和导航仍然在 php/Twig 中,但用户列表应该用 AngularJS 编写。 我已经启动了 AngularJS 前端并运行其文件结构,如下所示:
- 应用程序/
- js/
- app.js
- 部分/
- 用户.html
- user_detail.html
- index.html
- js/
- bower_components/
- ...
现在,我只想在 Symfony2 中的给定路径下显示它。 有没有办法说我不想在 Symfony 中为前端提供任何 Controller - 只是静态交付?
对于此类应用程序,一般合理的方法是什么? 我发现的大多数教程都假设整个网站都是用 Angular 编写的,而不仅仅是一个包。
感谢您的任何提示!
最佳答案
我之前使用过 asoc/assetic-angular-js-bundle
( GitHub ),当与 assetic 过滤器配置一起使用时,意味着所有部分和脚本都被合并到一个文件。
他们的 github 页面说要在你的 twig 模板中使用以下内容..
{% javascripts filter="angular"
'@BundleName/Resources/views/aTemplate.html.ng'
'@BundleName/Resources/views/fooTemplate.html.ng'
'@BundleName/Resources/views/moarTemplates/*.html.ng'
%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
但是如果你使用..
// app/config/config.yml
assetic:
filters:
angular:
apply_to: "\.ng$"
resource: %kernel.root_dir%/../vendor/asoc/assetic-angular-js-bundle
/Asoc/AsseticAngularJsBundle/Resources/config/assetic.xml
// This is all one line
// Not sure why this was needed, I vaguely remember
// assetic not being able to find to config
然后你就可以像这样在你的twig javascripts部分中调用你的partials..
{% javascripts
'@BundleName/Resources/views/angular-app.js'
'@BundleName/Resources/views/angular-controllers.js'
'@BundleName/Resources/views/some-other-stuff.js'
'@BundleName/Resources/views/aTemplate.html.ng'
'@BundleName/Resources/views/fooTemplate.html.ng'
'@BundleName/Resources/views/moarTemplates/*.html.ng'
%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
然后我想您可以将上述内容包含在您希望显示的一个 Twig View 中,也可以将其包含在所有模板中(取决于您想要如何处理脚本缓存以及不处理脚本缓存的方式)以及只需使用常规的 ng-view
UserListController
类型标签调用它即可。
很抱歉,如果这与您的问题无关。在我开始写这篇文章时,它非常有道理,但我仔细研究了它,看起来我已经完全偏离了主题。
关于javascript - Symfony2 - 与 AngularJS 捆绑在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23407037/