Angular 2 aot 无法绑定到 X,因为它不是 Y 的已知 属性

Angular 2 aot cannot bind to X since it isn't a known property of Y

参考下面的代码,无论我用<dform [meta]="frmeta"></dform>还是<dform meta="{{frmeta}}"></dform>都会报错

Can't bind to 'meta' since it isn't a known property of 'dform'."

正在编译打字稿代码。 有人知道我的代码有什么问题吗?

DformComponent.ts

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

@Component({
    selector:'dform',
    templateUrl:'DformComponent.html'
})
export class DformComponent{
  frmdata:any;

  constructor(fb:FormBuilder, @Attribute('meta') public meta:any){
    // this.frmdata = fb.group({
    //   phone:["123456789", containsMagicWord]
    //   , ip:["192.168.137.169", containsMagicWord]
    // });
    console.log(this.meta);
    debugger;
    this.frmdata = fb.group(this.meta);
  }

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

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>

RootComponent.ts

import { Component } from '@angular/core';

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'
})
export class RootComponent {
  frmeta:any = {
      phone:["123456789", containsMagicWord]
      , ip:["192.168.137.169", containsMagicWord]
  };
  constructor(){

  }
}

RootComponent.html

<dform meta="{{frmeta}}"></dform>

您需要将 DformComponent 添加到

@NgModule({
  ...,
  declarations: [DformComponent]

并且该组件需要

@Input() meta:SomeType;

为了

[meta]="frmeta"

有效。