[英]Understanding @input and @output in Angular2
For the example at url:对于 url 的示例:
http://ngcourse.rangle.io/handout/components/app_structure/two_way_data_binding.html http://ngcourse.rangle.io/handout/components/app_structure/two_way_data_binding.html
I am unable to understand how the following two lines function with respect to @Output关于@Output,我无法理解以下两行 function
//in app.component.ts
(countChange)="number2=$event"
(countChange)="onCountChanged($event)
//counter.component.ts
@Output() countChange: EventEmitter<number> = new EventEmitter<number>();
can someone help connect the dots here.有人可以帮助在这里连接点。
this.countChange.emit('foo');
in counter.component.ts
dispatches an event在
counter.component.ts
调度一个事件
(a bit similar to a click or other DOM event, but only handled in Angular internally) (有点类似于 click 或其他 DOM 事件,但仅在 Angular 内部处理)
(countChange)="onCountChanged($event)
registers a listener for this countChange
event.为这个
countChange
事件注册一个监听器。
It might be a bit confusing because Angular uses the same binding syntax for @Output()
as for DOM events.这可能有点令人困惑,因为 Angular 对
@Output()
与 DOM 事件相同的绑定语法。
When this.countChange.emit('foo');
当
this.countChange.emit('foo');
is executed the registered event handler is called and 'foo'
is passed (for the $event
argument).执行注册的事件处理程序被调用并传递
'foo'
(对于$event
参数)。
In simple terms,简单来说,
To trigger custom events
, EventEmitter
is used (generally from childcmp
to parentcmp
).要触发
custom events
,使用EventEmitter
(通常从childcmp
到parentcmp
)。 @Output
is a way to declare custom event name (type of EventEmitter
). @Output
是一种声明自定义事件名称( EventEmitter
类型)的方法。
In your case it is (from counter.component.ts ),在你的情况下是(来自counter.component.ts ),
@Output() countChange: EventEmitter<number> = new EventEmitter<number>();
says countChange
is a custom event (type of EventEmitter
).说
countChange
是一个自定义事件( EventEmitter
类型)。 EventEmitter
has methods like emit() , next() which can emit values. EventEmitter
有像emit() 、 next()这样的方法可以发出值。 So here it says countChange
can emit number value.所以这里说
countChange
可以发出数值。 eg.例如。
count=5;
buttonClick(){
this.countChange.emit(this.count); // will emit 5 value
}
Note whenever EventEmitter
(here countChange
) emits any ( number
) value, custom event(generally used in parentcmp
) will be triggered by its own.请注意,每当
EventEmitter
(此处为countChange
)发出任何( number
)值时,自定义事件(通常在parentcmp
)将由其自己触发。
In your case it is (from app.component.ts ),在你的情况下是(来自app.component.ts ),
(countChange)="number2=$event" //number2=5; //countChange is a custom event
automatically this.count
's 5 value will be received through $event and so it will be assigned to number2 .自动
this.count
的5值将通过$event接收,因此它将被分配给number2 。 which is also applicable for below code.这也适用于以下代码。
(countChange)="onCountChanged($event) //countChange is a custom event
somewhere written in component.某处写在组件中。
onCountChanged(value)
{
console.log(value); // =5;
}
I actually think Angular's own examples on how @Input
and @Output
work are pretty rubbish.我实际上认为 Angular 自己关于
@Input
和@Output
如何工作的示例非常垃圾。
Here is a simple StackBlitz example, which shows how it's done: https://angular-ivy-yqycev.stackblitz.io这是一个简单的 StackBlitz 示例,它显示了它是如何完成的: https://angular-ivy-yqycev.stackblitz.io
It looks like this:它看起来像这样:
When you type in characters in the "transmitter" textbox, it'll send your string as an @Output
... and the "receiver" will receive it via as an @Input
, and show it in it's own textbox.当您在“发送者”文本框中输入字符时,它会将您的字符串作为
@Output
... 发送,而“接收者”将通过@Input
接收它,并将其显示在自己的文本框中。
The key thing is that the "Transmitter" component will use an @Output
...关键是“Transmitter”组件将使用
@Output
...
transmitter.component.html:发射器.组件.html:
<div>
<h3>Transmitter</h3>
<input type="text" [(ngModel)]="value" (ngModelChange)="onChange($event)" />
</div>
transmitter.component.ts:发射器.component.ts:
@Output() emittter = new EventEmitter<string>();
onChange(event:any) {
this.emittter.emit(this.value);
}
And the "Receiver" component will use an @Input()
...并且“接收器”组件将使用
@Input()
...
receiver.component.html:接收器.component.html:
<div>
<h3>Receiver</h3>
<input type="text" [(ngModel)]="inputValue" />
</div>
receiver.component.ts:接收器.component.ts:
@Input() inputValue = "";
And, you can tie them together using this:而且,您可以使用以下方法将它们联系在一起:
app.component.html: app.component.html:
<app-transmitter (emittter)="onEmitter($event)"></app-transmitter>
<app-receiver [inputValue]="currentValue"></app-receiver>
app.component.ts: app.component.ts:
export class AppComponent {
currentValue = '';
onEmitter(event:any) {
console.log('Received: ' + event);
this.currentValue = event;
}
}
Hope this helps !希望这可以帮助 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.