【发布时间】:2016-10-25 23:26:21
【问题描述】:
编辑:想通了。我将index.html 正文中的所有html 移动到app.component.ts,一切正常。下面的答案是正确的方法。
我的问题是我有一个 ParentComponent,它从服务中读取数据,并将数据发布到侧边栏。当您单击侧栏中的某个项目时,它应该在页面的主要部分中发布有关该父级的详细信息。由于它需要两个不同的模板,我认为解决方案是使用父子关系,其中父是侧边栏,子是细节部分,我将数据传递给子显示。听起来对吗?有没有更好的方法来解决这个问题?我尝试了各种方法,但它们似乎都过时了,因为它使用了不再使用的directives。
编辑:另一个问题不一样,因为答案引用了directives,它们在 angular2 的 rc6 版本中被删除。这个问题是rc6后的问题。
编辑 2:添加了一些代码
index.html:
<header>
<div class="content">This is a header</div>
</header>
<div class="container">
<div class="sidebar">
<div class="content">
<parent-items></parent-items>
</div>
</div>
<div class="main-content">
<div class="content">
<my-app>Loading...</my-app>
<child-cmp [id]="selectedItem.id"></child-cmp>
</div>
</div>
<div class="clearfix"></div>
</div>
子-cmp.ts:
@Component({
selector: 'child-cmp',
})
export class ChildComponent {
@Input() set id(n) {
this.getData(n)
}
getData(id: number): void {
console.log('triggered');
};
}
父.ts:
@Component({
moduleId: module.id,
selector: 'parent-items',
templateUrl: `<ul class="items">
<li *ngFor="let item of items"
(click)="selectedItem = item"
[class.selected]="item === selectedItem">{{item.name}}</li>
</ul>`,
})
export class ItemsComponent implements OnInit {
items: Item[];
selectedItem: Item;
constructor(private itemService: ItemService) {};
getItems(): void {
this.itemService
.getItems()
.then(items => this.items = items);
}
ngOnInit(): void {
this.getItems();
}
}
【问题讨论】:
-
另一个问题不一样,因为答案引用了在 angular2 的 rc6 版本中删除的指令。这个问题是 rc6 之后的问题。
-
指令仍然是 angular2 的一部分。
-
如果您能提供更多信息,那就太好了。我所看到的一切都指向在 rc6 中删除的指令,而我不能在文档中删除它们。
-
只是谷歌 angular2 指令?很多文档。几乎所有东西都来自它们,比如组件。试试angular.io/docs/ts/latest/guide/attribute-directives.html
标签: angular typescript