Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度2需要将值设置为嵌套组件_Angular - Fatal编程技术网

Angular 角度2需要将值设置为嵌套组件

Angular 角度2需要将值设置为嵌套组件,angular,Angular,我试图将值设置为从json动态添加的嵌套componenet,但我无法实现 我尝试过的 我在另一个组件中创建一个嵌套组件,并在单击按钮时设置一个json值,我可以为name设置值,但不能为addresses和extra space对象数组设置值 请帮助我解决这个问题,如果这是一个重复的问题,很抱歉 App.component.html <div class="container"> <div class="row"> <div class="col-x

我试图将值设置为从json动态添加的嵌套componenet,但我无法实现

我尝试过的

我在另一个组件中创建一个嵌套组件,并在单击按钮时设置一个json值,我可以为name设置值,但不能为addresses和extra space对象数组设置值

请帮助我解决这个问题,如果这是一个重复的问题,很抱歉

App.component.html

<div class="container">
  <div class="row">
    <div class="col-xs-12">
      <div class="margin-20">
        <h4>Add customer</h4>
      </div>
      <form [formGroup]="myForm" novalidate (ngSubmit)="save(myForm)">
        <div class="form-group">
          <label>Name</label>
          <input type="text" class="form-control" formControlName="name">
          <small *ngIf="!myForm.controls.name.valid" class="text-danger">
              Name is required (minimum 5 characters).
            </small>
        </div>
        <!--addresses-->
        <div formArrayName="addressees">
          <div *ngFor="let address of myForm.controls.addressees.controls; let i=index" class="panel panel-default">
            <div class="panel-heading">
              <span>Address {{i + 1}}</span>
              <span class="glyphicon glyphicon-remove pull-right" *ngIf="myForm.controls.addressees.controls.length > 1" (click)="removeAddress(i)"></span>
            </div>
            <div class="panel-body" [formGroupName]="i">
              <address [group]="myForm.controls.addressees.controls[i]"></address>
            </div>
          </div> 
        </div>

        <div class="margin-20">
          <a (click)="addAddress()" style="cursor: default">
            Add another address +
          </a>
        </div>

        <div class="margin-20">
          <button type="submit" class="btn btn-primary pull-right" [disabled]="!myForm.valid">Submit</button>
        </div>
        <div class="clearfix"></div>

        <div class="margin-20">
          <div>myForm details:-</div>
          <pre>Is myForm valid?: <br>{{myForm.valid | json}}</pre>
          <pre>form value: <br>{{myForm.value | json}}</pre>
        </div>
      </form>
    </div>
  </div>
</div>

添加客户
名称
名称是必需的(至少5个字符)。
地址{i+1}
添加其他地址+
提交
我的表格详情:-
myForm有效吗?:
{{myForm.valid | json} 表单值:
{{myForm.value | json}
应用程序组件.ts

导出类AppComponent实现OnInit{
公共myForm:FormGroup;
构造函数(私有_fb:FormBuilder){}
恩戈尼尼特(){
this.myForm=this.\u fb.group({
名称:['',[Validators.required,Validators.minLength(5)],
收件人:此.\u fb.array([])
});
//添加地址
this.addAddress();
/*订阅地址值更改*/
//this.myForm.controls['addresses'].valueChanges.subscribe(x=>{
//控制台日志(x);
// })
}
initAddress(){
将此返回。\u fb.group({
街道:['',验证器。必需],
邮政编码:[''],
外部空间:this.\u fb.array([])
});
}
addAddress(){
const control=this.myForm.controls[“收件人];
const addrCtrl=this.initAddress();
控制按钮(addrCtrl);
/*订阅单个地址值更改*/
//addrCtrl.valueChanges.subscribe(x=>{
//控制台日志(x);
// })
}
removeAddress(i:编号){
const control=this.myForm.controls[“收件人];
控制。移除(i);
}
保存(型号:客户){
//调用API保存
// ...
console.log(模型);
}
}

有人能回答我的问题吗question@AJT_82你能帮我解决这个问题吗issue@GeorgeK您能帮我解决这个问题吗?我看不到有人试图添加任何数据?:)@AJT_82我不知道我的代码出了什么问题,我提供的链接也不起作用,对此表示抱歉
export class AppComponent implements OnInit {
    public myForm: FormGroup;

    constructor(private _fb: FormBuilder) { }

    ngOnInit() {
        this.myForm = this._fb.group({
            name: ['', [Validators.required, Validators.minLength(5)]],
            addressees: this._fb.array([])
        });

        // add address
        this.addAddress();

        /* subscribe to addresses value changes */
        // this.myForm.controls['addresses'].valueChanges.subscribe(x => {
        //   console.log(x);
        // })
    }

    initAddress() {
        return this._fb.group({
            street: ['', Validators.required],
            postcode: [''],
            extraspaces: this._fb.array([])
        });
    }

    addAddress() {
        const control = <FormArray>this.myForm.controls['addressees'];
        const addrCtrl = this.initAddress();

        control.push(addrCtrl);

        /* subscribe to individual address value changes */
        // addrCtrl.valueChanges.subscribe(x => {
        //   console.log(x);
        // })
    }

    removeAddress(i: number) {
        const control = <FormArray>this.myForm.controls['addressees'];
        control.removeAt(i);
    }

    save(model: Customer) {
        // call API to save
        // ...
        console.log(model);
    }
}