我正在使用角度1.5(建筑物的起始组件路径)从事新项目,但问题不是关于角度,而是关于设计模式。
假设我有这个主页的复杂网站:
<header></header>
<sidebar-left></sidebar-left>
<sidebar-right></sidebar-rigth>
<mainSection>
<news></news>
<movie-list></movie-list>
<random-quotes></random-quotes>
</mainSection>
<footer></footer>
新闻,电影列表和引用阻止从服务器显示数据。每个来自他们自己的方法/ api。那么世界卫生组织负责获取数据? main
组件应该获取数据并将其传递给子级吗?或news
,movieList
和randomQuotes
组件应自行获取数据?
1)Main
组件获取数据将其映射到范围并传递给子项
<mainSection>
<news items="$ctrl.news"></news>
<movie-list items="$ctrl.movies"></movie-list>
<random-quotes items="$ctrl.quotes"></random-quotes>
</mainSection>
VS
2)每个子组件都应该获取数据
class MovieListController {
constructor(MovieService) {
this.movieService = MovieService;
}
$onInit() {
this.movies = [];
this.movieService.getAll().then(response => this.movies = response);
}
}
export const movieList = {
template: '<movie-item ng-repeat="movie in $ctrl.movies"></movie-item>',
controller: MovieListController,
};