无法在Ionic Angular中删除文本字段的值

huangapple go评论59阅读模式
英文:

Unable to remove textfield value in Ionic Angular

问题

我正在处理一个使用Ionic和Angular制作的项目,我面临的问题是,每当管理员创建用户时,用户会收到一个注册链接。当用户打开链接时,会显示一个注册表单,然后我对其应用了所有验证,问题是浏览器会自动填充用户名字段,可能是缓存值,但在单击后它会得到验证,我希望在页面加载时对其进行验证,但值不在那里,它是空的。我已经附上了相同的屏幕截图。

在页面的任何地方单击后,验证会起作用。

有人知道我该怎么办吗?我希望在页面加载时显示验证,如果有错误的值。

这是Signup.html的代码:

<ion-label stacked>CHOOSE A USERNAME</ion-label>
<ion-item>
  <ion-input formControlName="username" trim="blur" id="username" type="text" (click)="validation()" (blur)="isUsernameUnique($event.target.value)"					 
    [class.invalid]="!signupForm.controls.username.valid && (signupForm.controls.username.dirty || submitAttempt)" (focus)="isTouched('username', true)"
    (focusout)="isTouched('username', false)">
  </ion-input>
  <ion-icon name="checkmark" item-right color="green" *ngIf="signupForm.controls['username'].valid && is_unique_username"></ion-icon>
</ion-item>				
<p class="error m-0-auto" col-12 ion-text color="danger" *ngIf="signupForm.controls['username'].hasError('required') && signupForm.controls['username'].touched && !signupForm.controls['username'].hasFocus">Username field is required.</p>
<p class="error m-0-auto" col-12 ion-text color="danger" *ngIf="signupForm.controls['username'].hasError('pattern') && (signupForm.controls['username'].touched || signupForm.controls['username'].untouched && !signupForm.controls['username'].hasFocus && signupForm.controls['username'].toString().trim() === '')">Please enter a valid username.</p>

以及Signup.ts的代码:

export class SignupPage implements AfterViewInit {
  @ViewChild('myInput', { read: ElementRef }) myInput: ElementRef;
  
  ngAfterViewInit() {
    if (this.myInput) {
      const inputElement = this.myInput.nativeElement as HTMLElement;
      this.renderer.setAttribute(inputElement, 'tabIndex', '0');
      inputElement.focus();
    }
  }
  
  ionViewDidEnter() {
    console.log("Username", this.signupForm.value['username']);
  }
}

请注意,我只提供了代码部分的翻译,没有额外的内容。

英文:

I'm Working on a project which is made in ionic and angular the issue i'm facing is whenever admin creates as user, user gets a signup link. when user opens a link a signup form shows then i'd applied all validations on it the issue is browser automatically fills the username field with it's may be cache value but after clicking it's get validated i want to validate it on page load but value is not present there it's empty.
i've attache screenshots fro the same.

无法在Ionic Angular中删除文本字段的值

after clicking anywhere on page

无法在Ionic Angular中删除文本字段的值

this validation works.

Anyone knows what can i do here. I want to show validation on page load if there is a wrong value

Here is the Code For Signup.html :-

&lt;ion-label stacked&gt;CHOOSE A USERNAME&lt;/ion-label&gt;
&lt;ion-item&gt;
&lt;ion-input formControlName=&quot;username&quot; trim=&quot;blur&quot; id=&quot;username&quot; type=&quot;text&quot; (click)=&quot;validation()&quot; (blur)=&quot;isUsernameUnique($event.target.value)&quot;					 

[class.invalid]=&quot;!signupForm.controls.username.valid &amp;&amp; (signupForm.controls.username.dirty || submitAttempt)&quot; (focus)=&quot;isTouched(&#39;username&#39;,true)&quot;
(focusout)=&quot;isTouched(&#39;username&#39;,false)&quot;&gt;
&lt;/ion-input&gt;&lt;ion-icon name=&quot;checkmark&quot; item-right color=&quot;green&quot; *ngIf=&quot;signupForm.controls[&#39;username&#39;].valid &amp;&amp; is_unique_username&quot;&gt;&lt;/ion-icon&gt;&lt;/ion-item&gt;				
&lt;p class=&quot;error m-0-auto&quot; col-12 ion-text color=&quot;danger&quot; *ngIf=&quot;signupForm.controls[&#39;username&#39;].hasError(&#39;required&#39;) &amp;&amp; signupForm.controls[&#39;username&#39;].touched &amp;&amp; !signupForm.controls[&#39;username&#39;].hasFocus&quot;&gt;Username field is required.&lt;/p&gt;&lt;p class=&quot;error m-0-auto&quot; col-12 ion-text color=&quot;danger&quot;*ngIf=&quot;signupForm.controls[&#39;username&#39;].hasError(&#39;pattern&#39;) &amp;&amp; signupForm.controls[&#39;username&#39;].touched || signupForm.controls[&#39;username&#39;].untouched &amp;&amp; !signupForm.controls[&#39;username&#39;].hasFocus &amp;&amp; signupForm.controls[&#39;username&#39;].toString().trim() === &#39;&#39;&quot;&gt;Please enter a valid username.&lt;/p&gt;

And For Signup.ts :-

export class SignupPage implements AfterViewInit{
@ViewChild(&#39;myInput&#39;, { read: ElementRef }) myInput: ElementRef;
ngAfterViewInit() {
	if (this.myInput) {
	  const inputElement = this.myInput.nativeElement as HTMLElement;
	  this.renderer.setAttribute(inputElement, &#39;tabIndex&#39;, &#39;0&#39;);
	  inputElement.focus();
	}
  }

ionViewDidEnter() {
	console.log(&quot;Username&quot;, this.signupForm.value[&#39;username&#39;]);
}

}

答案1

得分: 1

这个问题的问题在于浏览器管理如何将凭据放入字段。

也许你可以通过等待0.5秒并将焦点设置到输入框上,或者类似的操作来触发验证。

@ViewChild('myInput', {static:true}) myInput: IonInput;

--------

setTimeout(function(){
    this.myInput.setFocus();
}, 500);
英文:

The problem with this issue is that is the browser who manage how credentials are put into the fields.

Maybe you can workaround it by waiting 0.5 secs and setting the focus on the input or something like this to trigger the validations.

@ViewChild(&#39;myInput&#39;, {static:true}) myInput: IonInput;

--------

setTimeout(function(){
    this.myInput.setFocus();
}, 500);

huangapple
  • 本文由 发表于 2023年2月24日 11:56:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定