cant disappear placeholder after focus on it

淺唱寂寞╮ 提交于 2021-01-29 10:41:53

问题


Here I am using the angular material form for login form using Angular5. when the applications starts, the login form placeholder couldn't be over-written. But once one I logged in and logged out, then only the input fields would be over written. Screenshot

<form [formGroup]="loginform" class="login-form">
  <mat-icon style="font-size: 30px ">account_circle</mat-icon>
  <mat-form-field class="example-full-width">
    <input matInput placeholder="Username" formControlName="userName">
  </mat-form-field>
  <br>
  <mat-icon style="font-size: 30px">lock_open</mat-icon>
  <mat-form-field>
    <input matInput placeholder="Enter your password" [type]="hide ? 'password' : 'text'" formControlName="password">
    <mat-icon matSuffix (click)="hide = !hide">{{!hide ? 'visibility' : 'visibility_off' }}</mat-icon>
  </mat-form-field>
  <button mat-raised-button color="primary" [disabled]="!loginform.valid" (click)="clicklogin();loginform.reset()">Login</button>
  <button mat-raised-button color="primary">cancel</button>
</form>



CSS File

 .login-form{
    top: 35%;
    border: 1px;
    border-style: double;
    position: absolute;
    width: 30%;
    border-radius: 5px;
    padding: 20px;
    left: 30%;
}
mat-form-field{
    width: 80%;
}
button{
    text-align: center;
    margin-left :17%;
}
mat-icon{
    float: left;
    line-height: 2;
    margin-right: 10px;
}

回答1:


You can use mat-placeholder and handle visibility easily by CSS

styles:

.placeholder.hide-ph {
    display: none;
  }

component.ts:

<input 
  ...
  [value]="value"
  floatPlaceholder="never"
  (focus)="hidePlaceholder = true" // <== you need this
/>
 <mat-icon class="caret" matSuffix>arrow_drop_down</mat-icon>
 <mat-placeholder 
   class="placeholder" 
   [class.hide-ph]="!!value || hidePlaceholder" // <== and this to hide placeholder by css
 >
   {{ placeholder }}
 </mat-placeholder>



回答2:


Its working fine for me.

.login-form{
    top: 35%;
    border: 1px;
    border-style: double;
    position: absolute;
    width: 30%;
    border-radius: 5px;
    padding: 20px;
    left: 30%;
}
mat-form-field{
    width: 80%;
}
button{
    text-align: center;
    margin-left :17%;
}
mat-icon{
    float: left;
    line-height: 2;
    margin-right: 10px;
}
<form [formGroup]="loginform" class="login-form">
  <mat-icon style="font-size: 30px ">account_circle</mat-icon>
  <mat-form-field class="example-full-width">
    <input matInput placeholder="Username" formControlName="userName">
  </mat-form-field>
  <br>
  <mat-icon style="font-size: 30px">lock_open</mat-icon>
  <mat-form-field>
    <input matInput placeholder="Enter your password" [type]="hide ? 'password' : 'text'" formControlName="password">
    <mat-icon matSuffix (click)="hide = !hide"></mat-icon>
  </mat-form-field>
  <button mat-raised-button color="primary" [disabled]="!loginform.valid" (click)="clicklogin();loginform.reset()">Login</button>
  <button mat-raised-button color="primary">cancel</button>
</form>


来源:https://stackoverflow.com/questions/50770729/cant-disappear-placeholder-after-focus-on-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!