ham*_*161 5 html css algorithm typescript angular
我已经有一个正在运行的 Angular 应用程序,因为有功能扩展,我必须实现该应用程序的路由和登录页面。以前没有路由,作为路由的一部分,我添加了以下代码。
app.modules.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { SignUpComponent } from './sign-up/sign-up.component';
import { LoginComponent } from './login/login.component';
import {CommonModule} from "@angular/common";
import {FormsModule , ReactiveFormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent,
HomeComponent,
SignUpComponent,
LoginComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
CommonModule,
ReactiveFormsModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
注册.组件.ts
import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, FormsModule, ReactiveFormsModule} from '@angular/forms';
@Component({
selector: 'app-sign-up',
templateUrl: './sign-up.component.html',
styleUrls: ['./sign-up.component.css']
})
export class SignUpComponent implements OnInit{
public signupFrom : FormGroup|any;
constructor(private formBuilder: FormBuilder) {}
items : Array<any> = [
{name:"username", type:"text", data:"Username"},
{name:"password", type:"password",data:"Password"},
{name:"email", type:"text",data:"Email"}
]
ngOnInit(): void {
this.signupFrom = new FormGroup({
'Username' : new FormControl(),
'Passoword' : new FormControl(),
'Email' : new FormControl()
})
}
}
Run Code Online (Sandbox Code Playgroud)
注册.component.htm
<div class="row">
<div class="mx-auto col-10 col-md-8 col-lg-6">
<!-- Form -->
<form [formGroup]="signupFrom" class="form-example" >
<div *ngFor="let item of items " class="form-group" >
<label for="{{item.name}}">{{item.data}}</label>
<input
type="{{item.type}}"
class="form-control {{item.name}}"
id="{{item.name}}"
placeholder="{{item.data}}..."
name="{{item.data}}"
/>
</div>
<button type="submit" class="btn btn-primary btn-customized mt-4">
Sign Up
</button>
</form>
<!-- Form end -->
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
当我尝试使用 ng-serve 执行时,出现以下错误:无法绑定到“formGroup”,因为它不是“form”的已知属性。
我发现代码是正确的,除了以下几点:
模块永远不应该在组件中导入。
将您的 formGroup 声明为:(public signupFrom! : FormGroup! 对 typeScript 说“我知道表单首先可以为 null 或未定义”)。
一般来说,当我们在 app.module.ts 中导入新模块时,我们需要“重新启动”ng 服务
您应该在 .html 中使用绑定和 formControlName,否则输入不会与 formGroup 的 formControls“连接”。
请参阅 *ngIf 以避免初始错误
<!--the *ngIf avoid initial errors-->
<form *ngIf="signupForm" [formGroup]="signupFrom" class="form-example" >
<div *ngFor="let item of items " class="form-group" >
<label for="{{item.name}}">{{item.data}}</label>
<!--"better" use binding [ ] instead of interpolation {{ }}-->
<input
[type]="item.type"
[class]="'form-control '+item.name"
[id]="item.name"
[placeholder]="item.data+'...'"
[formControlName]="item.name"
/>
</div>
<button type="submit" class="btn btn-primary btn-customized mt-4">
Sign Up
</button>
</form>
Run Code Online (Sandbox Code Playgroud)
注意:请小心,创建 formGroup 时会写入您的信息
'Passoword' : new FormControl(), //<--should be Password (without the "o")?
Run Code Online (Sandbox Code Playgroud)