2013-11-27 2 views
0

레일스와 비슷한 방식으로 경로를 구조화하려고합니다.

$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가 맨 아래에 다음과 같습니다 : 경로가 /posts/new 또는 /posts/2 또는 /posts/2/edit

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가 인쇄 나는이와 비슷한 경로 설정이 있습니다. 컨트롤러가 적절한 조치를 취하도록 필터링하려면 무엇을 할 수 있습니까?

+0

99 % 각 경로마다 별도의 컨트롤러를 만들어야합니다. –

+0

그건 사실이 아닙니다. 다른 컨트롤러를 사용하는 이유는 범위에서 설정 한 데이터가 근본적으로 다른 경우입니다. 새로운 것, 편집하는 것, 사실상 동일하다는 것을 보여주기 위해 동일한 컨트롤러를 재사용하는 것이 합리적입니다. '목록'보기는 다를 수 있으며 자체 컨트롤러/템플릿임을 보증 할 수 있습니다. – dtabuenc

답변

1

레일즈에서 영감을 얻은 액션을 구현하는 더 쉬운 방법.

는 경로를 정의합니다

$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은 모든 "게시물"의 색인 위에 모달로 표시됩니다.

2

당신은 단지 해결 추가하여 쉽게 만들 수 있습니다

$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'); 
    } 
}); 
+0

'resolve'를 추가하는 것은 효과가 있지만, 실제로 달성되는 것에 대해 어리석은 것처럼 보입니다. 현재 라우트가 액션을 정의한다면'$ routeChangeSuccess'를보고'$ rootScope.action'을 설정합니다. – ravinggenius

+0

컨트롤러에 $ route를 주입 할 수 있습니다 – dtabuenc

관련 문제