【问题标题】:Having problems with the "type" property in Angular MaterialAngular Material 中的“type”属性存在问题
【发布时间】:2018-04-11 00:13:21
【问题描述】:

不添加属性,看起来应该:

1

当你添加它时,它看起来像这样:

2

它显示为一个文本框。

我目前的代码:

app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { LoginComponent } from './login/login.component';
import { SignupComponent } from './signup/signup.component';
import { ForgotPasswordComponent } from './forgot-password/forgot-password.component';
import { SignupViaemailComponent } from './signup-viaemail/signup-viaemail.component';

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { MatToolbarModule, MatFormFieldModule, MatButtonModule, MatCardModule, MatMenuModule,
         MatIconModule, MatInputModule, MatTabsModule, MatDialogModule, MatRadioModule } from '@angular/material';

import { FlexLayoutModule } from "@angular/flex-layout";

@NgModule({
  declarations: [
    AppComponent,
    LoginComponent,
    SignupComponent,
    ForgotPasswordComponent,
    SignupViaemailComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    ReactiveFormsModule,
    HttpModule,
    BrowserAnimationsModule,
    MatToolbarModule,
    MatFormFieldModule,
    MatButtonModule,
    MatCardModule,
    MatMenuModule,
    MatIconModule,
    MatInputModule,
    MatTabsModule,
    MatDialogModule,
    FlexLayoutModule,
    MatRadioModule
  ],
  providers: [],
  bootstrap: [AppComponent],
  entryComponents: [ForgotPasswordComponent, SignupViaemailComponent]
})
export class AppModule { }

login.component.ts:

import { Component, OnInit } from '@angular/core';
import { usuario } from "../_modelos/usuario.model";

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

import { ForgotPasswordComponent } from '../forgot-password/forgot-password.component';

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

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  email = new FormControl('', [Validators.required, Validators.email]);

  getErrorMessage() {
    return this.email.hasError('required') ? 'You must enter a value' :
           this.email.hasError('email') ? 'Not a valid email' :
           '';
  }

  hide = true;

  constructor(public dialog: MatDialog) {}

  ngOnInit() {}

  openForgotPasswordDialog() {
    let dialog = this.dialog.open(ForgotPasswordComponent);

  }

}

login.component.html:

<div fxLayout="column" fxLayoutAlign="center center" class="column-login">

  <div class="email-container">
    <mat-form-field>

      <input matInput placeholder="Email Address" [formControl]="email" required>
      <mat-error *ngIf="email.invalid">{{getErrorMessage()}}</mat-error>
    </mat-form-field>
  </div>

  <div class="password-container">
    <mat-form-field>

      <input matInput placeholder="Password" [type]="hide ? 'password' : 'text'">
      <mat-icon matSuffix (click)="hide = !hide">{{hide ? 'visibility' : 'visibility_off'}}</mat-icon>
    </mat-form-field>
  </div>

  <button mat-raised-button color="primary">Submit</button>
  <button mat-button (click)="openForgotPasswordDialog()">Forgot Password</button>

</div>

这就是导致我问题的原因:

[type]="hide ? 'password' : 'text'"

我从Angular Material官网得到了密码输入的代码。

【问题讨论】:

  • 代码应该可以工作,没有问题,请您为此创建 plnkr 吗?这样我们就可以调试了。
  • 有错误吗?

标签: angular angular-material angular4-forms


【解决方案1】:

如果您有默认显示的边框问题,可以使用此修复

.输入{

box-shadow: none;

}

【讨论】:

    猜你喜欢
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 2019-10-28
    • 1970-01-01
    • 2017-11-07
    • 2020-10-30
    • 2017-09-14
    相关资源
    最近更新 更多