我对角不太了解,我试图掌握关于使用URL路由和状态等方面的最佳实践。我有个计划。这是一个简单的问题,但我用它来掌握我能得到的东西。
假设我有两个完全独立的网页来显示福特汽车和丰田汽车的信息。当您访问页面时,您所拥有的只是汽车ID编号,所以您只需点击url "cars.com/info/id:198273918273“即可。使用angular.js的最佳方法是立即从url中删除id号,使用它查找car make,并显示适当的html页面,所有这些都不更改浏览器顶部显示的url?
发布于 2015-01-21 18:48:16
您可以在路由templateUrl中使用函数。
.when('/your_url/:car_id', {
templateUrl: function(attrs){
//Example of the login yours will be complex i guess :P
if(attrs.car_id == 1) { return 'template_ford.html' }
if(attrs.car_id == 2) { return 'template_toyota.html' }
},
controller : 'someController'
})
这样,您的模板就可以在呈现页面之前进行整理,并且不需要将用户发送到不同的url。
发布于 2015-01-21 18:04:26
假设您使用的是角用户界面路由器
$stateProvider
.state('car-get', {
url:'/info/{car_id}/',
controller: ['$scope','$stateParams',function($scope,$stateParams){
var car_id = $stateParams.car_id;
// now you can get your car info
// and bind it to your template.
$scope.car = myservice.getcarinfo(car_id) //Here goes your service or your api calls to get car info
}],
templateUrl: 'path_to_detail_car_page.html',
});
这里是一个很好的角用户界面路由器乞丐教程
https://stackoverflow.com/questions/28073507
复制相似问题