我正试图像Rails那样构造我的路线。我有一些类似于此的路径设置:
$routeProvider.when('/posts', {
controller: 'PostsCtrl',
templateUrl: '/views/posts.html'
});
$routeProvider.when('/posts/new', {
controller: 'PostsCtrl',
templateUrl: '/views/posts.html',
doNew: true
});
$routeProvider.when('/posts/:postID', {
controller: 'PostsCtrl',
templateUrl: '/views/posts.html'
});
$routeProvider.when('/posts/:postID/edit', {
controller: 'PostsCtrl',
templateUrl: '/views/posts.html',
doEdit: true
});PostCtrl的底部有以下内容:
if ($routeParams.doNew) {
console.log('action: new');
} else if ($routeParams.doEdit) {
console.log('action: edit', $routeParams.postID);
} else if ($routeParams.libraryID) {
console.log('action: show', $routeParams.postID);
} else {
console.log('action: index');
}action: show是在路径为/posts/new、/posts/2或/posts/2/edit时打印的。我可以过滤什么使控制器路由到适当的操作?
发布于 2013-12-02 15:44:59
我想出了一种更简单的方法来实现Rails激发的动作。
确定路线:
$routeProvider.when('/posts', {
templateUrl: '/views/posts/index.html',
controller: 'PostsCtrl'
});
$routeProvider.when('/posts/new', {
templateUrl: '/views/posts/index.html',
controller: 'PostsCtrl',
action: 'new'
});
$routeProvider.when('/posts/:postID', {
templateUrl: '/views/posts/index.html',
controller: 'PostsCtrl',
action: 'show'
});
$routeProvider.when('/posts/:postID/edit', {
templateUrl: '/views/posts/index.html',
controller: 'PostsCtrl',
action: 'edit'
});然后为$routeChangeSuccess添加一个事件处理程序
app.run(['$rootScope', '$route', function ($rootScope, $route) {
$rootScope.$on('$routeChangeSuccess', function (currentRoute, previousRoute) {
if ($route.current.action) {
$rootScope.action = $route.current.action;
}
});
}]);然后在您的控制器中,您可以在$scope.action上进行分支。
if ($scope.action === 'new') {
$scope.newPost();
} else if ($scope.action === 'show') {
Post.get($routeParams.postID).then($scope.showPost);
} else if ($scope.action === 'edit') {
Post.get($routeParams.postID).then($scope.editPosts);
}正常情况下,我可能会为这些路径设置单独的控制器,但在我正在构建的应用程序上,new、show、edit在所有"Posts“的索引上都显示在一个模态中。
发布于 2013-11-28 00:51:03
只要添加一个决心,就可以轻松地完成任务:
$routeProvider.when('/posts', {
controller: 'PostsCtrl',
templateUrl: '/views/posts.html'
resolve: {
action: function(){return 'list';}
}
});
$routeProvider.when('/posts/new', {
controller: 'PostsCtrl',
templateUrl: '/views/posts.html'
resolve: {
action: function(){return 'new';}
}
});等等。
然后,只需将action注入控制器:
controller('PostCtrl', function($scope, action){
if(action==='new'){
console.log('new');
}
});https://stackoverflow.com/questions/20255404
复制相似问题