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

后端 未结 2 447
说谎
说谎 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();
        }
    );
    

提交回复
热议问题