-1

我正在学习如何使用@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);
  }
}

项目详细信息.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>
4

1 回答 1

0

我观察到了你的问题。

我认为你误解了这个概念。

在您的示例中,您EventEmitter没有必要使用。因为如果您没有任何父/子情况,您可以通过简单的按钮单击来处理添加新项目。

所以,我重建了你的例子。

我添加了一个test组件,其中我添加了您的文本框。

test.component.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>

测试组件.ts

import { Component, EventEmitter, Output } from '@angular/core';

@Component({
  selector: 'test',
  templateUrl: './test.component.html'
})
export class TestComponent {
  @Output() newItemAdded = new EventEmitter<string>();

  title = 'InputOutputBindings';
  currentItem = 'TV';

  addNewItem(value: string) {
    this.newItemAdded.emit(value);
  }
}import { Component, EventEmitter, Output } from '@angular/core';

@Component({
  selector: 'test',
  templateUrl: './test.component.html'
})
export class TestComponent {
  @Output() newItemAdded = new EventEmitter<string>();

  title = 'InputOutputBindings';
  currentItem = 'TV';

  addNewItem(value: string) {
    this.newItemAdded.emit(value);
  }
}

然后我将test组件作为子组件包含在组件下app

app.component.html

<test (newItemAdded)="newItemAdded($event)"></test>
<h3>TV items: </h3>
<div>
  <ul *ngFor="let item of items">
    <li>{{item}}</li>
  </ul>
</div>

app.component.ts

import { Component, EventEmitter, Output } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  items = ['item1', 'item2', 'item3', 'item4'];

  newItemAdded(val : string) {
    console.log("onlyNewlyAddedItems:" + val);
    this.items.push(val);
  }
}

我已向您的父 ( app) 组件添加了一个项目列表。所以,现在我们可以使用EventEmitter,因为我们要显示新添加item到父 ( app) 组件的列表中。

test组件add new item上单击按钮时会发出事件,该事件由app组件捕获。然后我们将新项目添加到现有items数组中。

非常仔细地看演示。

在StackBlitz工作演示。

于 2021-04-03T19:36:41.437 回答