2013-03-29 23 views
2
當我想要去

什麼是autodirecting不完整url的角度方式?

http://www.koran-auf-deutsch.de/koran-deutsch/23-die-glaubigen-al-mominun/ 

和剛進入

http://www.koran-auf-deutsch.de/koran-deutsch/23 

我直接查看網址

。我想在我的角度應用程序中獲得類似的行爲 ,您會在哪裏注入該功能?有任何想法嗎?

+0

這不會在數據所在的API /服務器端處理嗎? –

回答

2
angular.module('app', []). 
    config(['$routeProvider', function($routeProvider) { 
    $routeProvider. 
     when('/:id', {controller: RedirectCtrl}). 
     when('/:id/:title', {templateUrl: 'partials/post.html', controller: PostCtrl}). 
     otherwise({redirectTo: '/404'}); 
}]); 

function RedirectCtrl($routeParam, $http) { 

    var post_id = $routeParams.id; 

    // get title by id 
    $http.get('/api_url_to_get_title_by_id').success(function (data) { 
     window.location = "/" + post_id + "/" + data.title; 
    }); 
} 

RedirectCtrl.$inject = ['$routeParams', '$http']; 
+1

很好的解決方案!儘管我認爲post_id需要使用$ routeParams.id而不是$ scope.id來設置? –

+0

是的,確切地說。只是修復它。 –

相關問題