我定义了以下路由:
app.config(function ($routeProvider) {
$routeProvider.when('/', { redirectTo: '/articles' });
$routeProvider.when('/articles', {
controller: "ArticleCtrl",
templateUrl: "/myApp/article/index"
});
$routeProvider.when("/articles/new", {
controller: "ArticleCtrl",
templateUrl: "/myApp/article/create"
});
});
这是我的控制器:
app.controller("ArticleCtrl", function($scope, $location, $log, articleService) {
$scope.currentArticle = null;
if ($location.path() == "/articles") {
$scope.articles = articleService.list();
}
$scope.saveArticle = function() {
articleService.save($scope.currentArticle);
}
});
关于if ($location.path() == "/articles")
;我这样做是因为当我的视图更改为“/ articles / new”路径时,该路径通过以下链接进行控制:
<a href="/myApp/#/articles/new">Add Article</a>
控制器正在调用我的服务以再次获取文章列表。几个问题: