简体   繁体   English

从父组件传递到子组件的对象始终为null

[英]Object passed from parent component to child component always null

Refer to the code below, although I've initialized root component property frmeta and added @Input decorator on property meta inside DformComponent , the child DformComponent still get a null from the root component inside DformComponent constructor . 请参阅下面的代码,虽然我已经初始化的根组件属性frmeta并添加@Input物业装饰metaDformComponent ,孩子DformComponent还是从内部的根组件得到一个空DformComponent constructor Is there anything wrong with my code? 我的代码有什么问题吗?

RootComponent 根组件

import { Component } from '@angular/core';
import { DformComponent } from './controls/DformComponent';

function containsMagicWord(c: any) {
  if(c.value.indexOf('magic') >= 0) {
    return {
      noMagic: true
    }
  }

  // Null means valid, believe it or not
  return null
}

@Component({
  selector: 'body',
  templateUrl: 'RootComponent.html',
  providers: [DformComponent]
})
export class RootComponent {
  frmeta:any = {
      phone:["123456789", containsMagicWord]
      , ip:["192.168.137.169", containsMagicWord]
  };
  constructor(){
    // this.frmeta has been initialized here
    // this log is before the DformComponent constructor log
    console.log(this.frmeta);
  }
}

RootComponent.html RootComponent.html

<dform [meta]="frmeta"></dform>

DformComponent.ts DformComponent.ts

import { Component, Attribute, Input, OnInit } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';

@Component({
    selector:'dform',
    templateUrl:'DformComponent.html'
})
export class DformComponent implements OnInit{
  frmdata:any;
  @Input() meta:any;
  constructor(fb:FormBuilder, @Attribute('meta') meta:any){
    // both meta & this.meta are always undefined & null
    this.meta = meta;
    console.log(meta);
    debugger;
    this.frmdata = fb.group(this.meta);
  }

  ngOnInit(){

  }

  dosubmit(event:any){
    console.log(this.frmdata.value);
  }
}

DformComponent.html DformComponent.html

<form [formGroup]="frmdata" (submit)="dosubmit($event)">
    <inputmask formControlName="phone" mask="(___) ___ - ___"></inputmask>
    <inputmask formControlName="ip" mask="___.___.___.___" ></inputmask>
    <button type="submit">Post</button>
    <pre>{{ frmdata.value|json }}</pre>
</form>

You cannot access the bound property in the constructor because it is not available yet. 您无法访问构造函数中的bound属性,因为它尚不可用。 Use the ngOnInit life cycle hook, and you'll be able to access it there. 使用ngOnInit生命周期钩子,您将可以在其中访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 2单元测试数据从父组件传递到子组件 - Angular 2 unit testing data passed from parent component to child component 为什么不能正确获取从父组件传递给子组件的值呢? - Why not get the value passed from the parent component to the child component correctly? 从 Angular 父组件传递给子组件的数据在子组件中未定义 - Data passed from an Angular parent component to a child component is undefined in child component 从父组件引用时,子组件值为 null - Child component values are null when referenced from parent component 将巨大的 object 从父组件传递给子组件 - Passing huge object from parent to child component Angular 绑定 object 从父组件到子组件 - Angular binding object from parent to child component 数据未从子组件 angular 8 传递给父组件 - Data not being passed to parent from child component angular 8 在提交时将对象从父组件传递到子组件 - Passing object from parent component to child component on submit Angular:在子组件中更新传递对象的属性会更新父组件,而不会更新其值,为什么? - Angular : Updating property of passed object in child component updates parent component, updating its value doesn't, why? 角度 | 父组件作为输入传递给子组件 - Angular | Parent component being passed as input to child
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM