Angular2中有没有办法以某种方式阻止使用EventEmitter的事件的默认值?
我有以下场景:
import {Component, Output, EventEmitter} from 'angular2/core'
@Component({
selector: 'my-component',
template: `<button (click)="clickemitter.emit($event); onClick($event)" [style.color]="color"><ng-content></ng-content></button>`
})
export class MyComponent {
@Output clickemitter: EventEmitter<MouseEvent> = new EventEmitter();
private color: string = "black";
constructor() {
}
private onClick(event: MouseEvent): void {
if(!event.defaultPrevented) //gets called before the observers of clickemitter
this.color = "red";
else this.color = "blue";
}
}
@Component({
selector: 'my-app',
providers: [],
template: `
<my-component (clickemitter)="$event.preventDefault()">Hello Angular</my-component>
`,
directives: [MyComponent]
})
export class App {
constructor() {
}
}
Run Code Online (Sandbox Code Playgroud)
我做了一个Plunker这一点,太: …
在 Angular2 中是否有可能在组件被销毁之前收到通知!?即当它即将被摧毁时。
我有一个容器组件,它在其子组件之一上保存一个 ViewContainerRef ,该组件将用于动态加载视图。如果容器组件本身被破坏(在我的例子中,由于父组件中的 *ngFor 指令),我想分离当前在 ViewContainerRef 中加载的视图并将其再次附加到另一个容器上。
事情是:在 ngOnDestroy() 生命周期挂钩中,ViewContainerRef 已经被清除,因此所有视图都被销毁,不再需要分离。