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中正确地导入。

相关内容

热门资讯

透视新版!aapoker怎么选... 透视新版!aapoker怎么选牌(透视)辅助(都是是真的有挂);1、打开软件启动之后找到中间准星的标...
透视存在!德普之星私人局辅助免... 透视存在!德普之星私人局辅助免费(透视)透视(果然真的是有挂)1、完成德普之星私人局辅助免费的残局,...
透视科技!aapoker ai... 透视科技!aapoker ai插件(透视)透视方法(总是真的有挂)1、首先打开aapoker ai插...
透视私人局!德普之星私人局透视... 透视私人局!德普之星私人局透视(透视)透视软件免费入口官网(其实有挂)德普之星私人局透视是一种具有地...
透视插件!aapoker怎么设... 透视插件!aapoker怎么设置提高好牌几率(透视)透视软件(都是存在有挂)1)aapoker怎么设...
透视透视!德普之星辅助工具如何... 透视透视!德普之星辅助工具如何打开(透视)透视(真是存在有挂);1、德普之星辅助工具如何打开机器人多...
透视黑科技!aapoker万能... 透视黑科技!aapoker万能辅助器(透视)插件下载(切实存在有挂)1、在aapoker万能辅助器a...
透视辅助!德扑圈透视(透视)透... 透视辅助!德扑圈透视(透视)透视辅助插件(一直真的是有挂);1、在德扑圈透视ai机器人技巧中,中转单...
透视代打!aapoker怎么提... 透视代打!aapoker怎么提高中牌率(透视)透视脚本入口(原来真的是有挂)暗藏猫腻,小编详细说明a...
透视透视!德普之星私人局辅助器... 透视透视!德普之星私人局辅助器(透视)私人局辅助器(真是真的是有挂)1、德普之星私人局辅助器ai辅助...