Angularjs + Symfony2 routing can't make it work

跟風遠走 提交于 2019-12-13 04:24:21

问题


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:

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 %}

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

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')
})

Here is my routing file:

_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: the controllers are almost empty, just rendering the twig template i'm looking for.

Currently, when I open /usermanager I get a blank page with just the title "user manager". What do I do wrong ?


回答1:


Your angular route is after the hash. that means that

 www.yoursite.com/usermanager

witch for the symfony router is /usermanager, for your angular app is just /

if you visit this

 www.yoursite.com/usermanager/#/usermanager

then angulars route will be /usermanager.

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 #.



来源:https://stackoverflow.com/questions/24597959/angularjs-symfony2-routing-cant-make-it-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!