Angular - different route, same template/controller,different loading method

后端 未结 2 448
说谎
说谎 2021-02-08 10:42

I want to use routes, but I always want to use same template & controller. I have routes like this:

**a/:albumid**

and

**i/         


        
相关标签:
2条回答
  • 2021-02-08 11:36

    Check out this article, it describes a way to do exactly what you want:

    http://www.bennadel.com/blog/2420-Mapping-AngularJS-Routes-Onto-URL-Parameters-And-Client-Side-Events.htm

    I've used the technique, it works well.

    In a nutshell, something like this for routing:

    $routeProvider
        .when("/a/:album_id", {
            action: "album.list"
        }).when("/i/:imgid", {
            action: "images.load"
        })
    

    Then in your controller you can access $route.current.action and do the appropriate thing. The trick is to create a function in you controller that does all the work (the article calls it render()) and then call that function when $routeChangeSuccess fires:

    $scope.$on(
       "$routeChangeSuccess",
       function( $currentRoute, $previousRoute ){
            // Update the rendering.
            render();
        }
    );
    
    0 讨论(0)
  • 2021-02-08 11:37

    I created a super simple directive to handle this that allows routes to be have more like Rails or Codeigniter routes where the controller method is in the route definition. The method name is set in the routeProvider.when options and the directive is set in the template for the route. See: https://stackoverflow.com/a/22714634/250991

    0 讨论(0)
提交回复
热议问题