用户单击按钮时如何在angular4中单击时显示消息

安尼尔

我在设计登录页面时感到震惊,我正在使用angular4 formmodule,我想在单击登录按钮时显示一些消息,但我无法做到这一点,请帮帮我。先感谢您。

这是我的html代码,

<div class="container">
    <h1>Hi Champ</h1>
    <form #heroForm="ngForm">
      <div class="form-group">
        <label for="name">Name</label>
        <input type="text" class="form-control" id="name"
       required>
      </div>

      <div class="form-group">
        <label for="password">Password</label>
        <input type="Password" class="form-control" id="password">
      </div>

      <div class="form-group">
      <label for="power">Role</label>
      <br>
      <select class="form-control" id="power" required>
      <option *ngFor="let pow of powers" [value]="pow">{{pow}}</option>
      </select>
</div>

      <button type="button" class="btn btn-default" (click)="signin()">SignIn</button>
    </form>
</div>

组件中的代码完全为空,请帮助我,

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

import { Hero } from '../hero';

@Component({
  selector: 'app-hero-form',
  templateUrl: './hero-form.component.html',
  styleUrls: ['./hero-form.component.css']
})
export class HeroFormComponent{


   powers = ['Admin', 'Looser'];

   signin(){
   console.log("Hello");
   }

}
阿伦·拉吉·R。

像这样更改您的<form> <button> 标记登录方法:

<form (ngSubmit)="signin(heroForm)" #heroForm="ngForm">  
<button type="submit" class="btn btn-default">SignIn</button>

import { NgForm } from '@angular/forms'; 
export class HeroFormComponent{
   signin(form: NgForm){
   console.log("Hello");
   }  
}

在继续之前,强烈建议您阅读使用ngSubmit提交表单,以更好地了解Angular表单概念。

并确保app.module.ts文件中包含FormsModule

import { FormsModule } from '@angular/forms';

@NgModule({ 
  declarations: [ ],
  imports: [ FormsModule ],
  providers: [ ]
})

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在单击按钮时显示隐藏的div并在Angular 2中隐藏单击的按钮?

单击凸起按钮时如何在吐司上显示消息?

单击按钮时如何显示消息?

当用户单击提交按钮时如何显示用户输入。当用户单击清除按钮时也清除消息。(Angularjs)

用户单击音频标签中的播放按钮时如何显示消息?

在Android中单击“禁用”按钮时如何显示消息?

Slack API:如何在单击按钮时在消息中添加用户头像?

如何在Windows Phone 7中单击按钮时显示弹出消息

如何在单击按钮时显示iframe

如何在单击按钮时显示模态

如何在单击按钮时显示DatePickerDialog?

如何在单击按钮时显示视图?

如何在php中单击按钮时显示表格?

如何在单击按钮时在uimageview中显示不同的图像

如何在角度4中单击浏览器后退按钮时提醒用户?

在 Angular 中,如何在单击时更改按钮的文本

在mvc4中单击按钮时未显示验证消息

当用户单击按钮时如何显示上传的图像?

如何在多个通知单击时显示适当的消息

如何在每次单击 jquery ajax 时显示 Flash 消息

在Install4j8中-如何在用户单击“取消”按钮时重定向到某些屏幕

如何在按钮单击时显示新的 HTML 表格

如何在单击单选按钮时显示hide ah:panelgroup

如何在按钮单击时应用显示和隐藏?

如何在按钮单击时显示加载程序

如何在单击按钮时显示通知提示?Windows Phone

如何在单击按钮时显示和隐藏局部视图?

如何在按钮单击时显示引导折叠 div?

Swift如何在按钮单击时显示标签栏