【发布时间】:2017-07-27 18:13:54
【问题描述】:
我想创建包含其他自定义控件的自定义控件,并使用连接所有这些控件的 ngModel。 喜欢:
人物搜索组件:
- 开发者搜索控件
- 名称 - 一些字符串输入
- 姓
- ProffesionSelect - 需要 ProffesionModel 的自定义控件
- 建筑搜索控件
- 这里有一些自定义控件
- CountryCustomControl - 需要 CountryModel 的自定义控件
- 人员列表组件: - 某些服务从 PersonSearchComponent 导入有关项目的数据
- SomeOtherSearch 组件
- DeveloperSearchControl - 可重复使用
所以现在我有工作版本,但我认为我做错了什么(也许我应该使用 FormBuilder):
人物搜索模板:
<div>
<developer-search-control [(ngModel)]="searchModel.developerSearchModel"></developer-search-control>
<building-search-control [(ngModel)]="searchModel.buildingSearchModel"></building-search-control>
<country-select [(ngModel)]="searchModel.countryModel"><country-select>
</div>
父搜索组件
...
export class PersonSearch {
@Output() searchDataEmitter= new EventEmitter();//Exports data to above component which contains this, and listComponent
searchModel : PersonSearchModel : new PersonSearchModel();
performSearch()
{
//gets data from service with searchModel and searchDataEmitter transpors it to above component
}
}
型号: 人物搜索模型:
developerSearchModel : DeveloperSearchModel = new DeveloperSearchModel();
buildingSearchModel: BuildingSearchModel = new BuildingSearchModel();
countryModel : CountryModel;
开发者搜索模型:
name : string
surname : string
proffesion : ProfessionModel
模板 developerSearchControl.component.html:
<div *ngIf="value">//This is the problem
<input [(ngModel)]="value.name"></input>
<input [(ngModel)]="value.surname"></input>
<profesion-select [(ngModel)]="value.ProffesionSelect">
</div>
developerSearchControl.component:
...
@Component({
selector: 'developer-search-control',
templateUrl: './developerSearchControl.component.html',
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => DeveloperSearchControlComponent),
multi: true,
}],
})
export class DeveloperSearchControlComponent extends ElementBase<DeveloperSearchModel > {
protected model: NgModel;
constructor(
@Optional() @Inject(NG_VALIDATORS) validators: Array<any>,
@Optional() @Inject(NG_ASYNC_VALIDATORS) asyncValidators: Array<any>,
) {
super(validators, asyncValidators);
}
}
profesionSelect.component
...
@Component({
selector: 'profesion-select',
template: `
<div>
<label *ngIf="label">{{label}}</label>
<dx-select-box
[dataSource]="data"
[(ngModel)]="value"
displayExpr="proffesionName"
[placeholder]="placeholder"
[searchEnabled]="true"
>
</dx-select-box>
</div>`,
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: ProfessionComponent,
multi: true,
}],
})
export class ProfessionComponent extends ElementBase<string> implements OnInit {
private data: ProfessionModel[];
private label: string = 'proffesion :';
private placeholder: string = 'Select profession';
@ViewChild(NgModel) model: NgModel;
constructor(private proffesionService: ProfessionDataService,
@Optional() @Inject(NG_VALIDATORS) validators: Array<any>,
@Optional() @Inject(NG_ASYNC_VALIDATORS) asyncValidators: Array<any>,
) {
super(validators, asyncValidators);
}
ngOnInit() {
this.professionService.getDevelopersProfessions().subscribe(
data => {
this.data = data;
}
);
}
}
ElementBase 是一个通用的 ControlValueAccessor,验证来自:http://blog.rangle.io/angular-2-ngmodel-and-custom-form-components/
所以我的问题是,当我为孩子(developerSearch、buildingSearch)创建模板时,没有为他们初始化通过 ngModel 传递的值,我得到:
EXCEPTION: Uncaught (in promise): Error: Error in ./DeveloperSearchControlComponent class DeveloperSearchControlComponent - inline template:2:33 caused by: Cannot read property 'name' of null
Error: Error in ./DeveloperSearchControlComponent class DeveloperSearchControlComponent - inline template:2:33 caused by: Cannot read property 'name' of null
因为 ngModel 的值在开始时为空。所以我必须在看起来很糟糕的子组件模板中使用 *ngFor="value" 。 有没有办法在模板验证之前初始化对象?还是我做错了?
【问题讨论】:
-
您是否在使用 value 的任何地方都尝试过 elvis 运算符?类似
<input [(ngModel)]="value?.name"></input>。至少,这会抑制异常。 -
@mickdev 安全导航运算符不能用于
[(ngModel)];) -
@AJT_82 我从未尝试过,但在我看来这是可能的。我不明白为什么不...
-
@mickdev 正如 AJT_82 所说,它会引发异常。已经试过了 :) 但它在代码中仍然看起来不太好。
-
@AJT_82 因为你不能分配给包含
?.的东西你需要将它拆分为[ngModel]="foo?.bar" (ngModelChange)="foo && foo.bar = $event"
标签: angular angular2-forms angular2-ngmodel