【问题标题】:angular2 need to access the child value in parent classangular2 需要访问父类中的子值
【发布时间】:2017-06-20 13:27:33
【问题描述】:

在这里,我需要访问父类中的子值。 但我无法得到它。如果我使用指令,它会显示错误。 有人可以帮我在父组件中显示子值以及如何以反应形式验证子值吗?

parent.html:

<browser  [template]="coreActivity" (onselect)="onselect($event)" ></browser>

parent.ts:

 onselect(select: any)
 {
    console.log(select.value);
 }

child.html:

<md-grid-list cols="3" rowHeight="100px">
    <md-grid-tile *ngFor="let core of template" (click)="selectcore(core)">
       {{core.value}}
     </md-grid-tile>
</md-grid-list>

child.ts:

@Component({
  selector: 'template-browser',
  templateUrl: './template-browser.component.html',
  styleUrls: ['./template-browser.component.css']
})
export class TemplateBrowserComponent implements OnInit {
  @Input() template;

  @Output() notify: EventEmitter<string> = new EventEmitter<string>();  
  constructor(private community: CreateCommunityComponent ) { }  
  selectcore(core: any) {
      // alert(core.value);
      this.notify.emit(core.value);
    }  
}

【问题讨论】:

    标签: angular2-forms


    【解决方案1】:

    当您通过EventEmitter 传递值时,您需要将其与parent.html 中的子组件声明一起传递。

    <browser (notify)="onSelect($event)"  [template]="coreActivity" (onselect)="onselect($event)" ></browser>
    

    并且在 parent.ts 下的函数中:

     onSelect(select)
     {
        console.log(select.value);
     }
    

    【讨论】:

    • 我能够将子值访问到父级。如何验证 使用反应表单验证??????当我尝试进行验证时,它给出了错误; ERROR 错误:没有名称的表单控件的值访问器:'模板'
    • 你能详细说明一下吗?如果可能的话,为此创建新线程
    【解决方案2】:

    为了访问父级中的child.html,您需要创建另一个组件并将该组件名称用作父级中的标记。 像组件名称是“孩子” 将&lt;child&gt;&lt;/child&gt; 保留在父级中

    【讨论】:

    • 您好,能否提供更多详细信息?也可能的代码
    猜你喜欢
    • 2016-03-16
    • 2016-02-02
    • 2016-09-18
    • 1970-01-01
    • 2017-05-21
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    相关资源
    最近更新 更多