Angular響應式表單之FormGroup

2021-10-22 18:35:07 字數 1321 閱讀 1191

import

from

'@angular/core'

;import

from

"@angular/forms"

;@component()

export

class

logincomponent

implements

oninit

)}

然後關聯這個formgroup到檢視中:

="form-signin"

[formgroup]

="frommodel"

(ngsubmit)

="onsubmit()"

>

"text" id=

"username" formcontrolname=

"username"

>

"text" id=

"password" formcontrolname=

"password"

>

"submit"

>sign in

<

/button>

<

/form>

formgroup和formcontrolname是響應式表單模組提供的指令。當我們用formgroup來管理一組表單控制項的時候,使用formcontrolname將控制項和表單繫結是乙個很方便的操作

我們處理表單的過程中,有時候會遇到很複雜的表單。angular提供了formbuilder服務來方便的生成表單控制項,使用起來更方便簡單

constructor

(private loginservice:loginservice,

private fb:formbuilder,

private router: router)

)}

重置:

this

.registerform.

reset()

;

獲取表單的值:

const value = name.value;
獲取姓名值:

const usernamevalues =

this

.frommodel.value.username;

獲取密碼值:

const passwordvalues =

this

.frommodel.value.password;

Angular 響應式表單之表單分組

表單提交,表單全部校驗成功才能提交,當表單校驗錯誤,表單邊框變紅,同時有錯誤提示資訊,有重置功能 git倉庫 在分析 之前,首先明確 formcontrol formcontrol formcontrolname formgroup formgroup formgroupname formarray...

Angular 響應式表單之表單分組

表單提交,表單全部校驗成功才能提交,當表單校驗錯誤,表單邊框變紅,同時有錯誤提示資訊,有重置功能 git倉庫 在分析 之前,首先明確 formcontrol formcontrol formcontrolname formgroup formgroup formgroupname formarray...

Angular 響應式表單之表單分組

表單提交,表單全部校驗成功才能提交,當表單校驗錯誤,表單邊框變紅,同時有錯誤提示資訊,有重置功能 git倉庫 在分析 之前,首先明確 formcontrol formcontrol formcontrolname formgroup formgroup formgroupname formarray...