简体   繁体   English

如何在单击按钮时将绑定变量的值传递给模板中的方法

[英]How to pass value of a binding variable to a method in the template on button clicked

I am learing how to make binding between the parent and the child using @Input, @Output and EventEmitter decorators.我正在学习如何使用@Input、@Output 和 EventEmitter 装饰器在父子之间进行绑定。 in the html section posted below在下面发布的 html 部分中

<h1 appItemDetails [item]="currentItem">{{currentItem}}</h1>

currentItem has value equal to "TV". and i pass this value to the binding variable item.

i added console.log in ngOnInit that prints the value of item to make sure that the binding from the parent to the child is working as suppose to be.我在 ngOnInit 中添加了 console.log 来打印 item 的值,以确保从父级到子级的绑定正常工作。

in

<button (click) = addNewItem(item.value)></button>

in this button tag i am trying to pass the value of the binding variable item to the method addNewItem() as a parameter.在此按钮标记中,我试图将绑定变量 item 的值作为参数传递给方法 addNewItem()。 For the method addNewItem() it exists in the component and it should be invoked with the right parameter which is the value of the binding variable item对于 addNewItem() 方法,它存在于组件中,应该使用正确的参数调用它,该参数是绑定变量 item 的值

when i compile the App i revceive the error posted below.当我编译应用程序时,我收到了下面发布的错误。 please let me know how to pass the value of the binding variable to a method on button clicked请让我知道如何将绑定变量的值传递给单击按钮的方法

error错误

TS2339: Property 'item' does not exist on type 'AppComponent'.

2 <button (click) = addNewItem(item.value)></button>
                               ~~~~

  src/app/app.component.ts:5:16
    5   templateUrl: './app.component.html',
                     ~~~~~~~~~~~~~~~~~~~~~~
    Error occurs in the template of component AppComponent.

app.component.ts : 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';

  @Output() newItemValue = new EventEmitter<string>();

  addNewItem(val : string) {
    this.newItemValue.emit(val);
    console.log("add new item");
  }
}

item-details.directive.ts :项目详细信息.directive.ts

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

@Directive({
  selector: '[appItemDetails]'
})
export class ItemDetailsDirective {

  @Input() item : string = "";

  constructor() { }
  ngOnInit() {
    console.log("ngOnInit->:" + this.item)
  }

}

app.coponent.html : app.coponent.html

<h1 appItemDetails [item]="currentItem">{{currentItem}}</h1>
<button (click) = addNewItem(item.value)></button>

you can add exportAs like below:您可以添加导出如下:

@Directive({
  selector: '[appItemDetails]'
  exportAs: 'customdirective',
})
export class ItemDetailsDirective {
....
}

and in your html, you can add a reference to directive and get the binded value:在您的 html 中,您可以添加对指令的引用并获取绑定值:

<h1 #test="customdirective" appItemDetails [item]="currentItem">{{currentItem}}</h1>
<button (click) = addNewItem(test.item)></button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM