[英]p-button and togglemask not render until clicking input
I am using p-button
and togglemask="true"
from PrimeNG.我正在使用 PrimeNG
p-button
和togglemask="true"
。 But they do not render in the html initially until clicked.但在点击之前,它们最初不会在 html 中呈现。 I want to know why and how to fix this!
我想知道为什么以及如何解决这个问题! Thank you!
谢谢!
// user-login.component.html
<main>
<form #loginForm="ngForm" (ngSubmit)="login(loginForm)">
<h2>Sign in</h2>
<div>
<span class="p-float-label">
<input id="username" type="text" pInputText class="p-inputtext-lg"
[(ngModel)]="username">
<label for="username">Username</label>
</span>
</div>
<div>
<span class="p-float-label">
<p-password id="password" class="p-inputtext-lg" [toggleMask]="true"
[(ngModel)]="password"></p-password>
<label for="password">Password</label>
</span>
</div>
<div>
<button pButton type="submit" label="Sign in"></button>
</div>
</form>
<div>
<span>New User?</span>
<button pButton type="button" [routerLink]="'/signup'" label="Create account" class="p-button-text"></button>
</div>
</main>
This is the initial page I have without clicking any button in the page.这是我没有点击页面中任何按钮的初始页面。
// password input without togglemask
<p-password _ngcontent-sqf-c60="" id="password" class="p-element p-inputwrapper p-inputtext-lg"><div><input pinputtext="" class="p-inputtext p-component p-element"><!--container--><!--container--><!--container--></div></p-password>
// button
<button _ngcontent-dxo-c60="" pbutton="" type="submit" label="Sign in" class="p-element" ng-reflect-label="Sign in"></button>
Only after clicking the input box, ng-untouched ng-pristine ng-valid
added to the <form>
would the button show with styling, like this .只有在点击输入框后,添加到
<form>
的ng-untouched ng-pristine ng-valid
按钮才会显示样式,如下所示。
// password input with togglemask in wrong position
<p-password _ngcontent-sqf-c60="" id="password" class="p-element p-inputwrapper p-inputtext-lg p-password-mask ng-untouched ng-pristine ng-valid" ng-reflect-toggle-mask="true">
<div ng-reflect-ng-class="[object Object]" class="">
<input pinputtext="" class="p-inputtext p-component p-element" ng-reflect-ng-class="[object Object]" type="password">
<!--bindings={
"ng-reflect-ng-if": "false"
}--><i ng-reflect-ng-class="pi pi-eye" class="pi pi-eye"></i><!--bindings={
"ng-reflect-ng-if": "true"
}--><!--bindings={
"ng-reflect-ng-if": "false"
}-->
</div>
</p-password>
// button
<button _ngcontent-ale-c60="" pbutton="" type="submit" label="Sign in" class="p-element p-button p-component" ng-reflect-label="Sign in">
<span class="p-button-label">Sign in</span>
</button>
After clicking the password input box I get the desired styling like this .单击密码输入框后,我得到了这样的所需样式。
// password input with desired togglemask
<p-password _ngcontent-sqf-c60="" id="password" class="p-element p-inputwrapper p-inputtext-lg p-password-mask ng-untouched ng-pristine ng-valid" ng-reflect-toggle-mask="true">
<div ng-reflect-ng-class="[object Object]" class="p-password p-component p-inputwrapper p-input-icon-right">
<input pinputtext="" class="p-inputtext p-component p-element p-password-input" ng-reflect-ng-class="[object Object]" type="password">
<!--bindings={
"ng-reflect-ng-if": "false"
}--><i ng-reflect-ng-class="pi pi-eye" class="pi pi-eye"></i><!--bindings={
"ng-reflect-ng-if": "true"
}--><!--bindings={
"ng-reflect-ng-if": "false"
}-->
</div>
</p-password>
Based on the error mentioned by skink , I fixed the problem by simply adding name
attribute in <input>
as follow:根据skink提到的错误,我通过简单地在
<input>
中添加name
属性来解决问题,如下所示:
<input id="username" type="text" pInputText
[(ngModel)]="username" name="username"required>
The reason using name
attribute is specified in angular doc .使用
name
属性的原因在angular 文档中指定。
I've reproduced the issue on StackBlitz , and there seems to be a quite unambiguous error (that you don't see?):我在StackBlitz上重现了这个问题,似乎有一个非常明确的错误(你没有看到?):
Error: If ngModel is used within a form tag, either the name attribute must be set or the form control must be defined as 'standalone' in ngModelOptions.
错误:如果在表单标签中使用 ngModel,则必须设置 name 属性,或者必须在 ngModelOptions 中将表单控件定义为“独立”。
Example 1: <input [(ngModel)]="person.firstName" name="first">示例 1:<input [(ngModel)]="person.firstName" name="first">
Example 2: <input [(ngModel)]="person.firstName" [ngModelOptions]="{standalone: true}">示例 2:<input [(ngModel)]="person.firstName" [ngModelOptions]="{standalone: true}">
Which is totally right, you're mixing reactive forms with normal forms.完全正确,您将反应性 forms 与普通 forms 混合。 If you meant to use normal forms, then the
form
element should be removed from the DOM and the form submission should be handled at the button level:如果您打算使用普通的 forms,则应从 DOM 中删除
form
元素,并应在按钮级别处理表单提交:
<!--<form #loginForm="ngForm" (ngSubmit)="login(loginForm)">-->
<button pButton type="submit" (onClick)="login()" label="Sign in"></button>
<!--</form>-->
If you meant to use reactive forms, the ngModel
bindings should be replaced with the formControlName
attributes:如果您打算使用反应式 forms,则应将
ngModel
绑定替换为formControlName
属性:
<input
id="username"
type="text"
pInputText
class="p-inputtext-lg"
formControlName="username"
/>
<p-password
id="password"
class="p-inputtext-lg"
[toggleMask]="true"
formControlName="password"
></p-password>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.