【发布时间】:2021-06-30 03:07:02
【问题描述】:
我正在学习如何使用 @Input、@Output 和 EventEmitter 装饰器在父子之间进行绑定。 在下面的代码中
@Output() newItemValue = new EventEmitter<string>();
我创建了一个事件发射器,它会在对字符串参数调用方法 addNewItem 时发出一个值。 在我调用的 addNewItem 方法的主体中
this.newItemValue.emit(val);
我想在 this.newItemValue 上绑定,所以我在 html 文件中做了以下操作
<button appItemDetails (click) = addNewItem(newItem.value)>add new item</button>
<p (newItemValue)="onlyNewlyAddedItems($event)"></p>
但是当我编译应用程序时,我收到了下面发布的错误。 请让我知道如何从模板中绑定 this.newItemValue
错误
Failed to compile.
src/app/app.component.html:4:40 - error TS2345: Argument of type 'Event' is not assignable to parameter of type 'string'.
4 <p (newItemValue)="onlyNewlyAddedItems($event)"></p>
~~~~~~
src/app/app.component.ts:5:16
5 templateUrl: './app.component.html',
~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component AppComponent.
app.component.ts:
import { Component, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'InputOutputBindings';
currentItem = 'TV';
items = ['item1', 'item2', 'item3', 'item4'];
@Output() newItemValue = new EventEmitter<string>();
addNewItem(val : string) {
this.newItemValue.emit(val);
console.log("add new item:" + val);
this.items.push(val);
console.log("add new item:" + this.items);
}
onlyNewlyAddedItems(val : string) {
console.log("onlyNewlyAddedItems:" + val);
}
}
item-details.directive.ts:
import { Directive, Input, Output, EventEmitter } from '@angular/core';
@Directive({
selector: '[appItemDetails]',
exportAs: 'customdirective'
})
export class ItemDetailsDirective {
@Input() item : string = "";
constructor() { }
ngOnInit() {
console.log("ngOnInit->:" + this.item)
}
ngOnChange() {}
}
app.coponent.html:
<h1 #test = customdirective appItemDetails [item]="currentItem">{{currentItem}} item</h1>
<label>Add an item: <input #newItem></label>
<button appItemDetails (click) = addNewItem(newItem.value)>add new item</button>
<p (newItemValue)="onlyNewlyAddedItems($event)"></p>
【问题讨论】:
-
您的
<p>标签不会引发名为newItemValue的事件。您很可能希望在按钮单击中将值设置为变量并使用插值显示该变量(例如{{newItemValue}})。在 Angular 中,使用[prop]或{{prop}}语法表示“绑定到显示值或输入”,(eventName)表示“绑定到事件或输出”。 -
@pascalpuetz 请您看一下上面的 app.component.ts...它已经回答了您的询问
-
@LetsamrIt,看看我的回答。
-
@LetsamrIt 我没有任何询问吗?首先引起我注意的问题是,您尝试将一个事件
(newItemValue)绑定到一个普通的html 标记<p>,它永远不会调度一个名为(newItemValue)的事件。做<p (newItemValue)="myFunc()">大致相当于document.getElementById('myPElement').addEventListener('newItemValue', () => this.myFunc())。请注意“大致”,它并不完全相同,但概念是。
标签: angular typescript angular-directive