我有一个ListComponent.在ListComponent中单击某个项目时,该项目的详细信息应显示在DetailComponent中.两者都在屏幕上同时出现,因此不涉及路由.
如何告诉DetailComponent单击ListComponent中的哪个项目?
我考虑过将一个事件发送到父(AppComponent),并让父设置在DetailComponent上使用@Input设置selectedItem.id.或者我可以使用具有可观察订阅的共享服务.
编辑:通过事件+ @Input设置所选项目不会触发DetailComponent,但是,如果我需要执行其他代码.所以我不确定这是一个可以接受的解决方案.
但是这两种方法看起来都比Angular 1的做法复杂得多,这种做法要么通过$ rootScope.$ broadcast或$ scope.$ parent.$ broadcast.
由于Angular 2中的所有内容都是组件,我很惊讶没有关于组件通信的更多信息.
有没有其他/更直接的方法来实现这一目标?
我正在使用Angular2并从官方网站下载了package.json.当我试图在@Component装饰器中使用"指令"时,我收到此错误.
我附上了我的代码错误:
[ts]
Argument of type '{ selector: string; template: string; directives: string;
}' is not assignable to parameter of type 'ComponentMetadataType'.
Object literal may only specific known properties, and 'directives' does not
exist in type 'ComponentMetadataType'.
(property) directives: string
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
import { Component } from '@angular/core';
import { PeopleListComponent } from './people-list/people-list.component';
@Component({
selector: 'my-app',
template: '<h1>{{ title }}</h1>',
directives: '' //ERROR //NOT ABLE TO RECOGNIZE
})
export class AppComponent {
title = "My title";
}
Run Code Online (Sandbox Code Playgroud)
这是我的package.json:
{
"name": …Run Code Online (Sandbox Code Playgroud) 我正在学习Angular2但是我对使用Angular2和MDL有疑问.为什么MDL导航栏不能与Angular2一起使用?当我使用带有标题和抽屉的导航栏时,抽屉不工作,所以我无法点击它,我看不到抽屉的图标.还有另一个问题:文本字段也无法正常工作.我想使用mdl-textfield - expandable(搜索)但是当我点击这个搜索字段时它没有扩展.但是,没有Angular2它工作正常.
UPDATE
<div class="demo-layout-waterfall mdl-layout mdl-js-layout">
<header class="mdl-layout__header mdl-layout__header--waterfall">
<!-- Top row, always visible -->
<div class="mdl-layout__header-row">
<!-- Title -->
<span class="mdl-layout-title">Title</span>
<div class="mdl-layout-spacer"></div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--expandable
mdl-textfield--floating-label mdl-textfield--align-right">
<label class="mdl-button mdl-js-button mdl-button--icon" for="waterfall-exp">
<i class="material-icons">search</i>
</label>
<div class="mdl-textfield__expandable-holder">
<input class="mdl-textfield__input" type="text" name="sample" id="waterfall-exp">
</div>
</div>
</div>
<!-- Bottom row, not visible on scroll -->
<div class="mdl-layout__header-row">
<div class="mdl-layout-spacer"></div>
<!-- Navigation -->
<nav class="mdl-navigation">
<a class="mdl-navigation__link" href="">Link</a>
<a class="mdl-navigation__link" href="">Link</a>
<a class="mdl-navigation__link" href="">Link</a>
<a class="mdl-navigation__link" href="">Link</a> …Run Code Online (Sandbox Code Playgroud)