Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="https://github.com/leeahoward/so-restful-routing/tree/master/app" rel="nofollow">Here is a project on github that does something close to what you are asking</a></p> <p>EDIT: I discovered something interesting that had not occurred to me before. If you leave out the controller in the route it will use the controller specified in the template. So as long as all the templates that you use for a given controller have ng-controller="resource" then it will load that controller for the template as expected. Of course with the current implementation of routes there are no optional parameters, so if you have two or three parameters you would need to specify a separate route. Biggest problem is it appears to call the controller method twice. I am guessing this is because there are two views with the same controller. However one view should replace the other so there should not be two calls. This seems like a bug to me. I also found some discussion of a possible new routing system in the works that may meet your needs, but it may be pretty far off: <a href="https://github.com/angular-ui/router/issues?page=1&amp;state=open" rel="nofollow">https://github.com/angular-ui/router/issues?page=1&amp;state=open</a>. The sample on github is now using the following method so you can browse that if you like. </p> <pre><code>var restrouteApp = angular.module('restrouteApp', []) .config(['$routeProvider', function($routeProvider) { $routeProvider .when('/:ctrl/:method', { templateUrl: function(rp){ if(!rp.method) {rp.method = 'index';} console.log('route one'); return 'views/'+rp.ctrl+'/'+rp.method+'.html'; } }) .when('/:ctrl/:method/:id', { templateUrl: function(rp){ if(!rp.method) {rp.method = 'index';} console.log('route two'); return 'views/'+rp.ctrl+'/'+rp.method+'.html'; } }) .otherwise({ redirectTo: '/resource1/' }); }]); </code></pre> <p>And the templates:</p> <pre><code>&lt;div ng-controller="resource1"&gt; &lt;h1&gt; resource1/one.html &lt;/h1&gt; &lt;div&gt;{{r1data.selected}}&lt;/div&gt; &lt;/div&gt; </code></pre> <p>Now in your controller you can do this to call the method dynamically.</p> <pre><code>restrouteApp.controller('resource1', function($scope,$routeParams,$log,Resource1Service) { $log.info('new resource1'); $scope.controllername = $routeParams.ctrl; $scope.r1data= Resource1Service.shared; $scope.index = function(){ Resource1Service.index().then(function(){ //when the service returns }); } $scope.one = function(){ $scope.r1data.selected = $scope.r1data.resources[0]; } $scope.two= function(){ $scope.r1data.selected = $scope.r1data.resources[1]; } //call the specified method of this controller $scope[$routeParams.method](); }); </code></pre> <p>/EDIT</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload