Forms 输入时不带表单标签的表单验证-角度2

Forms 输入时不带表单标签的表单验证-角度2,forms,angular,tags,Forms,Angular,Tags,我有一个弹出组件,我想在输入元素的名称(必填字段和minlength 3个字符)上插入表单验证。 不在表单标记中设置完整的代码,是否可以执行此操作 <div class="scenario-div"> <label style="font-size: 1.4rem; font-weight: 400; margin-bottom: 10px;">{{context.header}}</label> <div class="" style="width:

我有一个弹出组件,我想在输入元素的名称(必填字段和minlength 3个字符)上插入表单验证。 不在表单标记中设置完整的代码,是否可以执行此操作

<div class="scenario-div">
<label style="font-size: 1.4rem; font-weight: 400; margin-bottom: 10px;">{{context.header}}</label>
<div class="" style="width: 90%; margin-bottom: 10px; align-self: center;">
    <div style="display: flex; flex-direction: column; margin-bottom: 10px;">
        <label style="font-size: 1.2rem; font-weight: 500;">Name</label>
        <div style="display:flex; flex-direction:row;">
            <input type="text" [(ngModel)]="context.name" placeholder="enter Name" style="color: #569bd2; margin: 0px; width: 50%" minlength="3" required/>

            <select *ngIf="context.showOptionDropdown" class="custom-select-project" style="width:50%" #optionSelect (change)="context.option.value">
                <option disabled>Select type</option>
                <option value="option1">option1</option>
                <option value="option2">option2</option>
                <option value="option3">option3</option>
                <option value="option4">option4</option>
            </select>
        </div>
    </div>

    <div style="display: flex; flex-direction: column; margin-bottom: 10px;" *ngIf="!context.hideDescription">
        <label style="font-size: 1.2rem; font-weight: 400;">Description</label>
        <textarea rows="4" [(ngModel)]="context.description" cols="50" maxlength="250" placeholder="Enter text here" style="color: #569bd2; resize: none; font-weight: 400; font-size: 1.2rem;"></textarea>
    </div>
</div>
<div>   
    <button class="btn-main" (click)="confirm(context)" style="width: 15%;">Confirm</button>
</div>

{{context.header}
名称
选择类型
选择1
选择2
选择3
选择4
描述
证实

您需要使用反应式表单API中的
FormControl
,它可以与未包装到
标记中的独立
一起使用

Component({
   ...
   template: `<input type="text" [formControl]="name">` 
})
class MyComponent {
  name = new FormControl('',
           [Validators.required, Validators.minLength(3)]);
}
组件({
...
模板:``
})
类MyComponent{
名称=新表单控件(“”,
[Validators.required,Validators.minLength(3)];
}

基于ngModel的“验证”

    <input type="text" required name="title" [(ngModel)]="titleModel">
    <span style="color:red" *ngIf="titleModel?.length > 10">
          10 max
    </span>
    <span style="color:red" *ngIf="!titleModel">
          required
    </span>
    <button [disabled]="titleModel?.length > 10 || !titleModel">OK</button>

最多10个
必修的
好啊


基于本地模板变量的“验证”:

<input type="text" (keyup)='0' #title>
<span style="color:red" *ngIf="title.value.length > 10">
      10 max
</span>
<span style="color:red" *ngIf="!title.value">
      required
</span>

<button [disabled]="title.value.length > 10 || !title.value">OK</button>

最多10个
必修的
好啊

好的,我知道了,并为价值创建了跨度。长度工作正常,但我仍然能够完成提交。。。如果我的长度小于3个字符,我需要锁定提交。我在您的代码段中没有看到任何表单标记,但没有任何表单标记…这就是问题所在…我如何进行表单验证,但没有表单标记。不过,对你们来说,我可以设法隐藏提交按钮,直到用户输入超过3个字符:)若并没有表单,你们如何提交?你是说一个普通按钮上的动作?然后,根据条件隐藏按钮,或者使用if-elseSorry在类中检查模型/值长度…我的坏…对问题进行了一些编辑…现在将修复它