<label>用户名:</label>
<input type="text" #userNameRef=ngModel [(ngModel)]=userName required>
<span [style.color]="userNameRef.valid ? 'black':'red'">{{userNameRef.valid}}</span>
<div class="form-group">
<label>用户名:</label>
<input type="text" class="form-control" #userNameRef=ngModel minlength="3" maxlength="8" [(ngModel)]=userName required>
<span [style.color]="userNameRef.valid ? 'black':'red'">{{userNameRef.valid}}</span>
</div>
<div class="form-group">
<label>用户名:</label>
<input type="text" class="form-control" #userNameRef=ngModel minlength="3" maxlength="8" [(ngModel)]=userName required>
<span [style.color]="userNameRef.valid ? 'black':'red'">{{userNameRef.errors|json}}</span>
<div *ngIf="userNameRef.errors?.required">this is required</div>
<div *ngIf="userNameRef.errors?.minlength">should be 3 chacaters</div>
</div>
<span [style.color]="userNameRef.valid ? 'black':'red'" *ngIf="userNameRef.errors!=null">{{userNameRef.errors|json}}</span>
<form #form="ngForm" (ngSubmit)="form.form.valid && submit(form.value)" novalidate class="form-horizontal" role="form">
<div class="form-group" [ngClass]="{ 'has-error': form.submitted && !userName.valid }">
<label class="col-sm-2 control-label">用户名:</label>
<div class="col-sm-10">
<input required name="userName" [(ngModel)]="user.userName" #userName="ngModel" type="text" class="form-control" placeholder="请输入用户名...">
<div *ngIf="form.submitted && !userName.valid" class="text-danger">用户名必须输入!</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">密码:</label>
<div class="col-sm-10" [ngClass]="{'has-error': form.submitted && !password.valid }">
<input required minlength="8" maxlength="12" [(ngModel)]="user.password" name="password" #password="ngModel" type="password" class="form-control" placeholder="请输入密码...">
<div *ngIf="form.submitted && !password.valid" class="text-danger">密码必须输入,至少要8位!</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">登录</button>
</div>
</div>
</form>
import { Component, OnInit} from '@angular/core';
import{UserModel} from '../model/user.model';//引入了usermodel
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
constructor() { }
//定义user为Usermodel
private user=new UserModel();
ngOnInit() {
}
/**
* 登陆事件
* @param form 表单中的输入值
*/
submit(form){
if(form.username=="1"&&form.password=="12345678"){
alert("登录成功了");
}else{
alert("非法用户");
}
}
}
export class UserModel{
userName:string;
password:string;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有