我有一个表单,用户可以从多种类型的选择控件中选择多个域,当用户提交表单时,我会将所选域保存在另一个数组中并保存在会话中,所以如果用户返回表单,我需要获取所选域的选择选项。
HTML代码
<select clrSelect multiple name="linkDomain" ngModel class="listBox" required="">
<option *ngFor="let dom of domainList" [selected]="domain | domainFilter:dom.id" [ngValue]="dom.id">{{dom.domain}}</option>
</select>
如果我从选择控件中删除了ngModel
,那么我会选择一些选项,但是表单验证无效。
PIPE
@Pipe({ name: 'domainFilter' })
export class DomainFilter implements PipeTransform {
transform(domains: Domain[], compareValue: number) {
if (!domains || !compareValue) {
return;
}
domains.forEach(domain => {
if (domain.id === compareValue){
return true;
}
})
return false;
}
}
domains是一个数组,
domains = [{id: "1", domain: "domain1.com"},{id: "2", domain: "domain2.com"}]