GVKun编程网logo

angular5 – mat-select必需的验证不起作用(angular magnification)

7

在本文中,我们将带你了解angular5–mat-select必需的验证不起作用在这篇文章中,我们将为您详细介绍angular5–mat-select必需的验证不起作用的方方面面,并解答angular

在本文中,我们将带你了解angular5 – mat-select必需的验证不起作用在这篇文章中,我们将为您详细介绍angular5 – mat-select必需的验证不起作用的方方面面,并解答angular magnification常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的Angular 2表单验证,minLength验证器不起作用、Angular 9-Angular Material垫分页器不起作用、Angular Material UI中的Material Components在Angular Project中不起作用、Angular Multi-select在表单内部不起作用

本文目录一览:

angular5 – mat-select必需的验证不起作用(angular magnification)

angular5 – mat-select必需的验证不起作用(angular magnification)

我有以下代码

<form #createForm="ngForm">
 <mat-form-field>
   <mat-select placeholder="Favorite food"
      matInput
     [ngModel]
     food="food"
     #food="ngModel" required>
    <mat-option *ngFor="let food of foods" [value]="food.value">
      {{ food.viewValue }}
    </mat-option>
   </mat-select>
</mat-form-field>
</form>
<button [disabled]="!createForm.valid">submit</button>

由于我希望“选择”是必填字段,因此在呈现表单时应禁用“提交”按钮.但是,显示表单时会启用“提交”按钮.问题是什么?

解决方法

用于验证角度5使用反应形式. refer this

*** componentsnet.ts *******

import { FormControl,Validators,FormBuilder,FormGroup,ReactiveFormsModule,NgForm } from '@angular/forms';

export class Test implements OnInit{
foodform:FormGroup;
 constructor(){}

ngOnInit() {
// create form group of controls 
 this.foodform = new FormGroup({
   favoriteFood: new FormControl('',[Validators.required])
 });
}
}

**** Component.html ************

<form #createForm="ngForm" [formGroup]="foodform ">
     <mat-form-field>
       <mat-select placeholder="Favorite food"
          matInput
         [ngModel]
         food="food"
         #food="ngModel"  formControlName="favoriteFood">
        <mat-option *ngFor="let food of foods" [value]="food.value" >
          {{ food.viewValue }}
        </mat-option>
       </mat-select>
      <mat-error *ngIf="foodform.controls['favoriteFood'].hasError('required') && foodform.controls['favoriteFood'].pristine">
                required Message
      </mat-error>
    </mat-form-field>
    </form>

在html表单中使用[formGroup]和formControlName.

Angular 2表单验证,minLength验证器不起作用

Angular 2表单验证,minLength验证器不起作用

我有以下Angular 2形式:
<register>
    <form [ngFormModel] = "registrationForm">
        <div>
            <labelfor="email">Email</label>
            <inputtype="email" id="email" ngControl="email" #email="ngForm">
        </div>
        <div *ngIf = "email.touched && email.errors">
            <div *ngIf = "!email.errors.required && email.errors.underscoreNotFound">
                <span>Underscore is required</span> 
            </div>
            <div *ngIf = "email.errors.required">
                <span>Email is required</span>
            </div>
        </div>
        <div>
            <labelfor="password">Password</label>
            <inputtype="password" id="password" ngControl="password" #password="ngForm">
        </div>
        <div *ngIf = "password.touched && password.errors">
            <div *ngIf = "password.errors.minLength && !password.errors.required">
                <span>Password should contain 6 characters</span>
            </div>  
            <div *ngIf = "password.errors.required">
                <span>Password is required</span>
            </div>          
        </div>
    </form>
</register>

这是我实现验证器的组件:

import {Component} from '@angular/core';
import {Control,ControlGroup,FormBuilder,Validators} from '@angular/common';
import {CustomValidator} from './CustomValidator';

@Component({
    selector: 'register',templateUrl: './app/authentication/register_validation/register.html',})

export class RegisterComponent{
    registrationForm: ControlGroup;

    constructor(formBuilder:FormBuilder)
    {
        this.registrationForm = formBuilder.group({
            email: ['',Validators.compose([Validators.required,CustomValidator.underscore])],password: ['',Validators.minLength(6)])]
        });
    }

}

在这种形式下,电子邮件字段对两个验证器都正常工作,即当我不输入任何内容时,它会给出“需要电子邮件”的消息,当我开始输入内容时,它会给出“需要下划线”的消息,当我输入“_”时所有错误消息都消失了.但是,当我尝试在密码字段上应用这样的2个验证器时,它不起作用.当我不输入密码时,它会显示“需要密码”的消息.但是当我键入少于6个字符的内容时,minLength消息根本不会出现.这段代码有什么问题?

error key is minlength而不是minLength:
<div *ngIf = "password.hasError('minlength') && !password.hasError('required')">
  <span>Password should contain 6 characters</span>
</div>

Angular 9-Angular Material垫分页器不起作用

Angular 9-Angular Material垫分页器不起作用

我通过更新角度/材质解决了该问题。另外,我已经改变了

import { MatTableDataSource } from '@angular/material';

 import { MatTableDataSource } from '@angular/material/table';

它解决了我的问题。希望对其他人有帮助。

Angular Material UI中的Material Components在Angular Project中不起作用

Angular Material UI中的Material Components在Angular Project中不起作用

如何解决Angular Material UI中的Material Components在Angular Project中不起作用?

我正在尝试在有角度的项目中使用材料组件,即垫子形式字段,垫子自动完成。材料按钮工作正常,但其他组件不工作。下面的邮政编码和错误-

错误-

Error Screenshot

<mat-form-field>
    <!-- #docregion input -->
        <input type="text"
               placeholder="Pick one"
               aria-label="Number"
               matInput
               [matAutocomplete]="auto">
    <!-- #enddocregion input -->
    <!-- #docregion mat-autocomplete -->
        <mat-autocomplete #auto="matAutocomplete">
            <mat-option *ngFor="let category of categoryList" [value]="category">
                {{category}}
              </mat-option>
        </mat-autocomplete>
    <!-- #enddocregion mat-autocomplete -->
</mat-form-field>

package.json ---->

"dependencies": {
"@angular/animations": "~10.1.0","@angular/cdk": "^10.1.0","@angular/common": "~10.1.0","@angular/compiler": "~10.1.0","@angular/core": "~10.1.0","@angular/forms": "~10.1.0","@angular/material": "^10.1.0","@angular/platform-browser": "~10.1.0","@angular/platform-browser-dynamic": "~10.1.0","@angular/router": "~10.1.0","rxjs": "~6.6.0","tslib": "^2.0.1","zone.js": "~0.10.2"
}

将几乎所有建议的模块都导入了app.module.ts --->

import { MatFormFieldModule,MAT_FORM_FIELD_DEFAULT_OPTIONS } from ''@angular/material/form-field'';
import { browserAnimationsModule } from ''@angular/platform-browser/animations'';
import {MatAutocompleteModule} from ''@angular/material/autocomplete'';
import {MatInputModule} from ''@angular/material/input'';
import { NgModule,CUSTOM_ELEMENTS_SCHEMA } from ''@angular/core'';
import { AppRoutingModule } from ''./app-routing.module'';
import {FormsModule,ReactiveFormsModule} from ''@angular/forms'';
import { AppComponent } from ''./app.component'';

imports: [
browserModule,AppRoutingModule,FormsModule,ReactiveFormsModule,MatAutocompleteModule,MatButtonModule,MatInputModule,MatFormFieldModule,browserAnimationsModule
],providers: [
 { provide: MAT_FORM_FIELD_DEFAULT_OPTIONS,useValue: {floatLabel: ''auto''}}
],schemas: [CUSTOM_ELEMENTS_SCHEMA],

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

Angular Multi-select在表单内部不起作用

Angular Multi-select在表单内部不起作用

首先,如果要在form标签中使用 formControlName ,则需要为ng-select添加 formControlName , 为了选择多个值,您需要添加[multiple] = true

 <ng-select [items]="bItems" (change)="updateSelectedValues();" bindLabel="item_name"  [(ngModel)]="selectedBillerItems" [multiple]="true" placeholder="Select Category" formControlName="bItems"></ng-select>

您可以参考https://www.npmjs.com/package/@ng-select/ng-select链接以获取更多信息。

,

我有一个解决方案,解决方案非常简单。我只是向ng-select元素添加了一个name属性,问题就解决了。这是代码

<ng-select [items]="bItems" (change)="updateSelectedValues();" bindLabel="item_name"  [(ngModel)]="selectedBillerItems" [multiple]="true" placeholder="Select Category" name="something" formControlName="bItems"></ng-select>

我们今天的关于angular5 – mat-select必需的验证不起作用angular magnification的分享已经告一段落,感谢您的关注,如果您想了解更多关于Angular 2表单验证,minLength验证器不起作用、Angular 9-Angular Material垫分页器不起作用、Angular Material UI中的Material Components在Angular Project中不起作用、Angular Multi-select在表单内部不起作用的相关信息,请在本站查询。

本文标签: