각도 모양 일 때 <input ...>
태그가있는 템플릿의 맞춤 지시문에 문제가 있습니다.Angular2 - 양식 오작동 내 사용자 지정 지시문
입력 내용을 양식에 선언하면 입력 필드를 편집하면 pristine, touch, valid 등의 양식 속성이 예상대로 변경됩니다. 폼 내부에 정의 지시어를 선언하면
는 <ac2-string-input ...></ac2-string-input>
말하고는 템플릿이 입력의 필드를 편집이라면, 그것은 폼의 속성을 변경하지 것, 입력을 포함합니다.
왜 그럴까요? 그게 버그 야? 이 문제를 해결할 수있는 방법이 있습니까? 아래에서
는 예를 들어 있습니다 :/form.component.ts 가우리는 응용 프로그램에서 폼 구성 요소를 가질 수를 템플릿으로
import { Component } from '@angular/core'
import { InputComponent } from './input.component'
@Component({
selector: 'ac2-form',
templateUrl: '<build path>/templates/form/form.html',
directives: [InputComponent]
})
export class FormComponent {
item: Object = {
attr1: 'blah',
attr2: 'another blah'
}
constructor(){}
submit(){ console.log(this.item) }
}
서식/양식/form.html
<form #f="ngForm" (ngSubmit)="submit()">
<input type="text" name="attr1" [(ngModel)]="item.attr1"/>
<ac2-string-input [obj]="item"></ac2-string-input>
<button type="submit">Submit</button>
</form>
<div>
Pristine? {{f.form.pristine}}
</div>
그리고 AC2 문자열 입력 D irective는 응용 프로그램/양식/input.component.ts 템플릿과
import { Component, Input } from '@angular/core'
@Component({
selector: 'ac2-string-input',
templateUrl: '<build path>/templates/form/input.html'
})
export class InputComponent {
@Input() obj: Object;
constructor(){}
}
서식/양식에 정의되어 우리가 양식을로드하면/input.html
<input type="text" name="attr2" [(ngModel)]="obj.attr2"/>
,이있을 것 두 개의 텍스트 필드 및 양식
"깨끗한"입니다 "attr2"필드를 편집하면 필드가 양식에 바인딩되지 않은 것처럼 계속 양식이 깨끗합니다. 우리는 "ATTR1"필드를 편집하는 경우 예상대로
는, 양식, 깨끗한되지 않습니다.
"Component"를 사용할 때와 마찬가지로 html을 삽입해야합니다. "지시어"로 그것을 할 수있는 방법이 있습니까? 예를 들어, '
'의 속성 지시문을 사용하여 ''을 생성하고 양식을 사용합니까? – Aleksandrus