如何在Angular中从自定义标记获取属性值

如何在Angular中从自定义标记获取属性值,angular,typescript,Angular,Typescript,我正在尝试用angular5制作自己的灵活密码组件。我应该获取min-max属性值以验证密码,但无法获取密码 我曾尝试使用js(getAttribute)获取值,但它不起作用 <password [id]="passwordFieldId" title="Password field" placeholder="Enter a password"

我正在尝试用angular5制作自己的灵活密码组件。我应该获取min-max属性值以验证密码,但无法获取密码

我曾尝试使用js(getAttribute)获取值,但它不起作用

<password
                    [id]="passwordFieldId"
                    title="Password field"
                    placeholder="Enter a password"
                    [isValid]="isPasswordFieldValid"
                    [isDisabled]="isPasswordFieldDisabled"
                    [isRequired]="true"
                    type="password"
                    [minlength]= '5'
                    [maxlength] = '10'
                    [formControlName]="passwordFieldId"
                    [errorText]="errorText">
</password>



 public ngOnInit() {

        this.form = new FormGroup({
            [this.passwordFieldId]: new FormControl('', [Validators.required, Validators.minLength(/** i don`t know what should i write here */), Validators.maxLength(/** i don`t know what should i write here */)])
        });
    }

给我一个未定义的

我在这里展示了一个演示表单。希望这对你有帮助

Ts文件 1.进口

import{FormGroup, FormBuilder, Validators} from '@angular/forms';
2.课堂内 表格:表格组; 3.内部建造商

constructor() {

 this.form = this.formbuilder.group({
      passwordFieldId:['',Validators.compose([Validators.minLength(1), Validators.maxLength(6)])]
});
}
4.在HTML中

<form [formGroup]="form">
<input type="password" formControlName="passwordFieldId" >
<button (click)="getPsw()"></button>
</form>

这里我展示了一个演示形式的角度。希望这对你有帮助

Ts文件 1.进口

import{FormGroup, FormBuilder, Validators} from '@angular/forms';
2.课堂内 表格:表格组; 3.内部建造商

constructor() {

 this.form = this.formbuilder.group({
      passwordFieldId:['',Validators.compose([Validators.minLength(1), Validators.maxLength(6)])]
});
}
4.在HTML中

<form [formGroup]="form">
<input type="password" formControlName="passwordFieldId" >
<button (click)="getPsw()"></button>
</form>

您可以使用组件参考访问
maxlength

从模板中:

<password
 #passwordInput
 [id]="passwordFieldId"
...


{{ passwordInput.maxlength | json }}

您可以使用组件参考访问
maxlength

从模板中:

<password
 #passwordInput
 [id]="passwordFieldId"
...


{{ passwordInput.maxlength | json }}

为什么要把它弄得这么复杂?您只需在组件中定义两个全局变量:

public minlength: number = 5;
public maxlength: number = 10;
this.form = new FormGroup({
            [this.passwordFieldId]: new FormControl('', [Validators.required, Validators.minLength(this.minlength), Validators.maxLength(this.maxlength)])
        });
并在模板中同时使用它们

<password [minlength]="minlength" [maxlength]="maxlength"
</password>

为什么要把它弄得这么复杂?您只需在组件中定义两个全局变量:

public minlength: number = 5;
public maxlength: number = 10;
this.form = new FormGroup({
            [this.passwordFieldId]: new FormControl('', [Validators.required, Validators.minLength(this.minlength), Validators.maxLength(this.maxlength)])
        });
并在模板中同时使用它们

<password [minlength]="minlength" [maxlength]="maxlength"
</password>

谢谢这真的很有用,谢谢。它真的很有用