Angular指令如何从NgModel或FormControlName中获取值
创始人
2024-10-30 23:00:16
0

在Angular中,可以通过使用@ViewChild装饰器来获取NgModel或FormControlName指令的值。下面是一个示例代码:

HTML模板:




组件类:

import { Component, ViewChild } from '@angular/core';
import { NgModel } from '@angular/forms';

@Component({
  selector: 'app-example',
  template: `
    
    
  `
})
export class ExampleComponent {
  myValue: string;

  @ViewChild('myInput', { static: false })
  ngModelDirective: NgModel;

  getValue() {
    console.log(this.ngModelDirective.value);
  }
}

在这个示例中,@ViewChild装饰器用于获取ngModel指令,并将其赋值给名为ngModelDirective的变量。然后,可以在getValue方法中访问ngModelDirective.value来获取指令的值。

注意:这个示例假设ExampleComponent已经导入了NgModel,并且FormsModule已经在AppModule中正确地导入。

相关内容

热门资讯

第5分钟辅助!hhpoker有... 第5分钟辅助!hhpoker有没有作弊挂,wepoker到底有没有透视,法门教程(竟然有挂)亲,关键...
4分钟辅助!werplan怎么... 4分钟辅助!werplan怎么透视,wepoker脚本下载,举措教程(有挂存在)1)wepoker脚...
第十分钟辅助!wepoker私... 第十分钟辅助!wepoker私人局俱乐部怎么进,wepoker透视有吗,练习教程(有挂教程)wepo...
6分钟辅助!pokerrrr2... 6分钟辅助!pokerrrr2辅助,哈糖大菠萝免费辅助器,举措教程(有挂猫腻)1)哈糖大菠萝免费辅助...
五分钟辅助!wepoker底牌... 五分钟辅助!wepoker底牌透视,淘宝买wepoker透视有用吗,教程书教程(揭秘有挂)所有人都在...
9分钟辅助!wepoker轻量... 9分钟辅助!wepoker轻量版有透视吗,wepoker祈福有用吗,总结教程(有挂规律)1、玩家可以...
七分钟辅助!wpk辅助器,we... 七分钟辅助!wpk辅助器,wepoker有辅助功能吗,方针教程(有挂详细)1、七分钟辅助!wpk辅助...
6分钟辅助!来玩app破解版,... 6分钟辅助!来玩app破解版,wepoker辅助器免费,方针教程(有挂细节)1、在wepoker辅助...
五分钟辅助!模拟器打开hhpo... 五分钟辅助!模拟器打开hhpoker,hhpoker辅助器,教程书教程(竟然有挂)1、下载好模拟器打...
六分钟辅助!来玩app破解,德... 六分钟辅助!来玩app破解,德州局脚本,方式教程(有挂教程)1、进入到德州局脚本是否有挂之后,能看到...