简体   繁体   English

angular :将输入值传递给组件

[英]angular : passing the input value to the component

I want to affect the value of this input to a variable 'numToAdd' which is in my component and then add 1 to the variable 'numToAdd', but I can't pass the value of the html input to my component variable 'numToAdd'.我想将此输入的值影响到我组件中的变量“numToAdd”,然后将 1 添加到变量“numToAdd”,但我无法将 html 输入的值传递给我的组件变量“numToAdd” . How to bind my html input to a variable in the component ?如何将我的 html 输入绑定到组件中的变量?

my html code我的 html 代码

<input type="number" id=num class=num>

my component我的组件

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

@Component({
  selector: 'app-afficher-projets',
  templateUrl: './afficher-projets.component.html',
  styleUrls: ['./afficher-projets.component.css']
})
export class AfficherProjetsComponent implements OnInit {

  ngOnInit(): void {}

  numToAdd: number;
  
  constructor() { }

  add: void {
    this.numToAdd++;
  }

}

Two-way binding a primary functionality of angular.双向绑定 angular 的主要功能。 Use the ngModel directive to bind input to variables.使用ngModel指令将输入绑定到变量。

<input type="number" id="num" class="num" [(ngModel)]="numToAdd">

The above code binds the variable numToAdd with the input.上面的代码将变量numToAdd与输入绑定。 Find more details here在此处查找更多详细信息

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

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