如何使用子组件

时间:2017-09-06 13:39:32

标签: javascript angular angular2-forms angular-ngselect

我正在构建一个应用程序,我在其中构建了一个可以适应和文本输入,选择,文本区域等的输入组件......

所以我为此创建了一个模型,在那里我发送了所需的所有信息,因此组件可以适应我的需要。我也使用formCOntrol而不是ngModel表单(在阅读了一些最佳选项的页面之后)

我遇到的问题是我无法预先选择ng-select的值(之前我使用的是选择组件,它正在播种。

所以在创建表单中,我有20个不同类型的输入,一切都很好...当我尝试执行某些操作的编辑操作时会出现问题,因为我需要预先填充原始输入的所有输入值。对于所有组件都可以,因为我将formControlName与我想要更新的实体的模型共享,但是对于ng2-select我得到以下错误:

ERROR TypeError: selectedItems.map is not a function

我搜索了可能的解决方案,但没有一个对我有用..也许正如我在另一个组件中使用ng-select ....

所以这是我的代码。 这是路线:

ngOnInit() {
    this.supplierService.get(this.item.id).subscribe( (response : JsonResponse)=> {
      if(response.success){
        this.item = response.data;
        this.item.logo = this.supplierService.getImage(this.item);
        this.getState();

      }
  else{
    this.error = response.code;
  }
});

} 如您所见,首先我得到项目(要编辑的对象)。然后我得到状态(将在ng-select

中显示为选项的项目

在我获得状态后,我创建了formControl:

this.form = new FormGroup({
                name: new FormControl(this.item.name,Validators.required),
                last_name: new FormControl(this.item.last_name, Validators.required),
                fantasy_name: new FormControl(this.item.fantasy_name, Validators.required),
                real_name: new FormControl(this.item.real_name, Validators.required),
                state: new FormControl(this.item.state ? this.item.state : '', Validators.required),
                city: new FormControl(this.item.city ? this.item.city : '', Validators.required),
                phone: new FormControl(this.item.phone),
                fax: new FormControl(this.item.fax),
                discount: new FormControl(this.item.discount),
                address: new FormControl(this.item.address),
                apartment: new FormControl(this.item.apartment),
                cuit: new FormControl(this.item.cuit, Validators.required),
                email: new FormControl(this.item.email, Validators.required),
                floor: new FormControl(this.item.floor),
                mobile_phone : new FormControl(this.item.mobile_phone)
              });

最后,我为每个输入创建了组件,这里有状态示例:

let state : InputForm = new InputForm();
    state.setSelect("state", 'COMPONENT.COMMON.STATE', this.states, 6);
    this.formElement.elements.push(state);

在那里我设置了这个组件将是一个select,我的第一个参数是formControlName中的名称(这样,它知道哪个是formCOntrolName所以它可以将此值设置为默认值或在提交时分配新值然后是标签,然后是要显示的项目列表。(最后一个参数是输入的大小)。

这是我的组件对应于select:

的模板
<ng-select *ngIf="element.type == 'select' && (element.items && element.items.length > 0)" (typed)="typed(element.id, $event)" formControlName="{{element.id}}" [allowClear]="true" [items]="element.items" (selected)="change(element.id, $event)" id="{{element.id}}" (removed)="removed($event)" (typed)="typed($event)" placeholder="{{element.placeholder | translate}}"></ng-select>

我尝试了[active],[data],[initData],但没有任何效果。我总是得到错误

ERROR TypeError: selectedItems.map is not a function

我不知道我是否应该以其他方式分配默认值。错误在行

this._active = selectedItems.map(function (item)

在这种情况下,selectedItems是State类型(我的自定义类型)我不知道在此之前我是否应该进行转换。该类型具有id和文本值。

总之,错误只是在&#34;编辑&#34;动作..我的意思是,当元素有一个预先选定的值时,如果没有它可以正常工作。

我使用角度4.3.3

从失败的行看下图:

enter image description here

在那里你可以看到发送了对象,并且map函数也是未定义的。

1 个答案:

答案 0 :(得分:1)

您收到错误,因为selecteItems不是数组。

在您的代码中

this.form = new FormGroup({
                name: new FormControl(this.item.name,Validators.required),
                last_name: new FormControl(this.item.last_name, Validators.required),
                fantasy_name: new FormControl(this.item.fantasy_name, Validators.required),
                real_name: new FormControl(this.item.real_name, Validators.required),
                state: new FormControl(this.item.state ? this.item.state : '', Validators.required),
                city: new FormControl(this.item.city ? this.item.city : '', Validators.required),
                phone: new FormControl(this.item.phone),
                fax: new FormControl(this.item.fax),
                discount: new FormControl(this.item.discount),
                address: new FormControl(this.item.address),
                apartment: new FormControl(this.item.apartment),
                cuit: new FormControl(this.item.cuit, Validators.required),
                email: new FormControl(this.item.email, Validators.required),
                floor: new FormControl(this.item.floor),
                mobile_phone : new FormControl(this.item.mobile_phone)
              });

您需要将state定义更改为:

state: new FormControl(this.item.state ? [this.item.state] : '', Validators.required)

将项目设置为数组将删除错误,并允许您使用map进行迭代。