Javascript 使用AngularJS在Symfony2中布线
我在前端有用于RESTAPI和AngularJS的Symfony2。不知道如何处理路由。我有以下设置,但不起作用: 在Symfony2控制器中:Javascript 使用AngularJS在Symfony2中布线,javascript,php,angularjs,symfony,Javascript,Php,Angularjs,Symfony,我在前端有用于RESTAPI和AngularJS的Symfony2。不知道如何处理路由。我有以下设置,但不起作用: 在Symfony2控制器中: /** * @Route("/", name="index") * @Route("/post/{slug}", name="post") * @Template() */ public function indexAction() { // index.html.twig is a plain html file with inclu
/**
* @Route("/", name="index")
* @Route("/post/{slug}", name="post")
* @Template()
*/
public function indexAction()
{
// index.html.twig is a plain html file with inclusion of all the scripts
return $this->render('AcmeAppBundle::index.html.twig');
}
角度设置:
angular.module('acmeApp', [])
.config(['$routeProvider',
function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: '/assets/partials/index.html',
controller: "MainCtrl"
})
.when('/post/:slug', {
templateUrl: '/assets/partials/post.html',
controller: "PostCtrl"
})
.otherwise({
redirectTo: '/'
});
}
])
.config(['$locationProvider',
function($locationProvider) {
$locationProvider.html5Mode(true).hashPrefix('!');
}
])
当我打开acme.com/
并点击链接到acme.com/post/123
时,这一切都很好,Angular会选择路线。但是当我从acme.com/post/123
开始时,Symfony2重定向到acme.com/post
,而我没有进入post页面
有没有建议如何在Symfony2中设置路由以与Angular JS一起使用
更新:
将
添加到index.html.twig
似乎可以解决问题。Symfony中的路由与AngularJS
中的路由不同。例如,http://example.org/post/123#!/
在Symfony上是/post/123
,但在Angular上是/
。@DavidRiccitelli有意义。你知道如何进行路由吗?只要/post/123
返回id为123的帖子的JSON表示,以及/post
帖子数组,你就可以创建一个角度应用程序,使用$resource
加载帖子列表和帖子细节,查看此处了解更多详细信息:查看此处和此处@DavidRiccitelli,但Symfony不是要拦截路线/post/123
,并将其解释为/
?