上下文 用户可以注册一个唯一的 URL slug 来标识他们的页面,例如“ http://example.com/slug ”。
当前状态 在我的 Express.js 文件中,我成功检查了我的数据库以查看用户是否存在 slug,然后将用户从“ http://example.com/slug ”重定向到“ http://example.com/ #!/slug ' 以利用 Angular 的路由。
但是,对于 Angular,我不能在我的路由器文件中使用 $http 或 $location 服务(因为它发生在 module.config 中......有关更多详细信息,请参阅此 Stack Overflow 说明)。
欲望 基本上我想做的是在找到有效的 slug 时将用户路由到“默认”视图,或者如果没有,则将用户路由到“默认”视图。任何建议将不胜感激。
作为参考,我的 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'
});
}
]);