コンテキスト ユーザーは、' http://example.com/slug ' など、自分のページを識別する固有の URL スラッグで登録できます。
現在の状態 Express.js ファイルで、ユーザーにスラッグが存在するかどうかをデータベースで確認し、ユーザーを「http://example.com/slug」から「http://example.com/ 」にリダイレクトすることに成功しました。 #!/slug ' Angular のルーティングを利用します。
ただし、Angular では、ルーター ファイルで $http または $location サービスを使用できません (module.config 内で行われているため、詳細については、このスタック オーバーフローの説明を参照してください)。
欲求 基本的に私がやりたいことは、有効なスラッグが見つかったときにユーザーを「デフォルト」ビューにルーティングするか、そうでない場合はホームにルーティングすることです。どんな提案でも大歓迎です。
参考までに、私のmodule.configコードはここにあります(使用したい「デフォルト」状態は「検索」であることに注意してください):
core.client.routes.js
'use strict';
// Setting up route
angular.module('core').config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
// Redirect to home when route not found.
$urlRouterProvider.otherwise('/');
// Home state routing
$stateProvider.
state('home', {
url: '/',
templateUrl: 'modules/core/views/home.client.view.html'
}).
state('search', {
url: '/search',
templateUrl: 'modules/core/views/search.client.view.html'
});
}
]);
私がやりたいことは、このようなものです...
'use strict';
// Setting up route
angular.module('core').config(['$stateProvider', '$urlRouterProvider', '$http', '$location',
function($stateProvider, $urlRouterProvider, $http, $location) {
// Get current slug, assign to json.
var slug = $location.path();
var data = {
link: slug
};
// Check db for slug
$http.post('/my/post/route', data).success( function(response) {
// Found slug in db
}).error( function(response) {
// Route to home
$location.path('/');
});
// Home state routing
$stateProvider.
state('home', {
url: '/',
templateUrl: 'modules/core/views/home.client.view.html'
}).
state('search', {
// Set URL to slug
url: '/' + slug,
templateUrl: 'modules/core/views/search.client.view.html'
});
}
]);