AngularJS: multiple edit forms routing?

删除回忆录丶 提交于 2019-12-13 05:07:28

问题


I have a listing of Requests each with a Request_Type_Id which maps to a feeder list (which could grow as the application evolves). For example, we might have (id, request_type) 1 - Sales Information, 2 - Technical Support, 3 - Order Information and, of course, each one has unique fields so the edit forms are very different.

By clicking the name, I want to open the correct of these different edit forms.

For routing, I have this:

$routeProvider
    .when('/editrequest/:req_id/:id', {
       controller: 'EditRequestController',
       templateUrl: '/App/Views/editRequest.html'      
    }).otherwise( { redirectTo: '/' } );

because I set the edit link to

<a href="#/edit/{{req.request_Id}}/{{req.id}}">{{req.title}}</a>

I am still trying to learn AngularJS and am wondering if the best way to proceed from here would be to choose the correct edit form on /App/Views/editRequest.html by using ng-show. This would mean there would be at least 3 forms on that template (not nested, though) which might be problems in some browsers.

Is there a more AngularJS way to do this that I haven't learned yet?

Thanks in advance!


回答1:


templateUrl can be a string or a function, which accepts route parameters and return a string.

In your case, you can doing so:

$routeProvider
.when('/editrequest/:req_id/:id', {
   controller: 'EditRequestController',
   templateUrl: function(params) {
     // use params.req_id to generate a url
     return '/App/Views/editRequest'+params.req_id+'.html';
   }
}).otherwise( { redirectTo: '/' } );

Another important thing worth mention here is, be careful when use interpolate in href or src. Use ng-href or ng-src instead.



来源:https://stackoverflow.com/questions/23258817/angularjs-multiple-edit-forms-routing

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