Angularjs + Symfony2的路由不能使它工作 [英] Angularjs + Symfony2 routing can't make it work
问题描述
我一直在学习AngularJS,我想它与我的SF2后端集成。
我得到的路线混淆,让我们看看我有什么:
I've been learning AngularJS and I'd like to integrate it with my SF2 back end. I'm getting confuse with the routes let's see what I have:
当我们去到/的UserManager symfony的路由系统打开加载一个树枝模板的控制器。在此模板,我需要加载的角所有的JS组件和做到这一点:
When we go onto /usermanager the symfony routing system opens a controller that loads a twig template. In this template, I load all the js components needed for angular and do this:
{% extends '::base.html.twig' %}
{% block javascripts %}
{{ parent() }}
<script type="text/javascript" src="{{ asset('js/UserManager_Angular.js')}}"></script>
{% endblock %}
{% block body %}
<h1>User Manager</h1>
<div data-ng-view></div>
{% endblock %}
现在这是我的角度code:
Now this is my angular code:
var UserManager = angular.module('UserManager', ['ngRoute','ngResource']);
UserManager.config(function($routeProvider){
$routeProvider
.when('/addnew',
{
controller:'addNewController',
templateUrl:Routing.generate('_UserManager_add_new_user')
})
.when('/',
{
controller:'UserManagerHome',
templateUrl:Routing.generate('_UserManager_getUser_List')
})
});
UserManager.factory('data_access',['$resource',
function($resource){
return $resource(Routing.generate('_UserManager_getUserList'),{},{
query:{method:'GET',isArray:true}
});
}]);
UserManager.controller('UserManagerHome',function ($scope,data_access){
$scope.users = data_access.query();
//$scope.orderProp = 'id';
});
PS:你可以看到我用的是FOSBundle为JS路线曝光。我想我怎么用,因为我厂得到正确的用户数据库。
ps: as you can see I use the FOSBundle for js route exposure. I think I got how to use that, because my factory correctly gets the users from the database.
所以我想加载包含经典的棱角的东西的文件,如 NG-重复
等,这是重新通过这条路线psented $ P $:
So I want to load a file containing the classic angular stuff such as ng-repeat
etc. This is represented by this route:
.when('/usermanager',
{
controller:'UserManagerHome',
templateUrl:Routing.generate('_UserManager_userList')
})
下面是我的路由文件:
_UserManager:
pattern: /usermanager
defaults: {_controller: AcmeBundle:UserManager:Home }
options:
expose: true
_UserManager_getUser_List:
pattern: /usermanagerlist
defaults: {_controller: NRtworksSubscriptionBundle:UserManager:list }
options:
expose: true
_UserManager_getUserList:
pattern: /usermanager/getuserlist
defaults: {_controller: AcmeBundle:UserManager:getUserList }
options:
expose: true
requirements:
_format: json
_method: GET
_UserManager_add_new_user:
pattern: /usermanager/addnew
defaults : {_controller: AcmeBundle:AddNewUser:Home }
options:
expose: true
PS2:控制器都几乎是空的,只呈现树枝模板我在找
ps2: the controllers are almost empty, just rendering the twig template i'm looking for.
目前,当我打开 /的UserManager
我得到的只有标题用户管理空白页。我该怎么办错了?
Currently, when I open /usermanager
I get a blank page with just the title "user manager". What do I do wrong ?
推荐答案
您棱角分明的路线是哈希后。这意味着
Your angular route is after the hash. that means that
www.yoursite.com/usermanager
女巫symfony的路由器/的UserManager,为您的角度应用就是
/
witch for the symfony router is /usermanager, for your angular app is just /
如果你访问这个
www.yoursite.com/usermanager/#/usermanager
然后angulars路线将/的UserManager。
then angulars route will be /usermanager.
话说,棱角分明的路由是从你的Symfony的路线系统完全不同的环境,他们不应该有冲突。由于角路由的#之后发生的。
Saying that, angular's routing is a completely different environment from your Symfony's route system and they should not have conflicts. Because angular route is taking place after the #.
这篇关于Angularjs + Symfony2的路由不能使它工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!