使用模态时,从另一个组件调用属性时,Angular 6给出错误“ TypeError:无法读取属性”

时间:2018-11-13 13:50:21

标签: angular modalpopup mdbootstrap

我是新手,所以当我尝试从组件B调用组件A的当前值时,如果我犯了愚蠢的错误,我深表歉意。它给出了错误。我试图在弹出模式形式处于活动状态时调用这些值。单击打开的弹出窗口后,我试图调用函数“ editUserData()”,但它给出了错误。

组件A

import { Component, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';
import { userRegistrationData } from 
'src/app/models/registrationUser.models';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { EditUserDetailComponent } from '../editUser/edit-user-detail/edit- 
user-detail.component';
@Component({
selector: 'app-registration-form',
templateUrl: './registration-form.component.html',
styleUrls: ['./registration-form.component.css']
})
export class RegistrationFormComponent implements OnInit {

constructor(private httpClient: HttpClient ) { }


public currentValue: string;


editUserData(value:any): void
{
  this.currentValue = value;
}

}
  

组件B

import { Component, OnInit,ViewChild, ViewChildren, AfterViewInit, 
ElementRef, HostListener, Input, Injectable, AfterViewChecked, 
AfterContentInit, ContentChild } from '@angular/core';
import {ModalDirective} from 'mdbootstrap';
import { userRegistrationData } from 
'src/app/models/registrationUser.models';
import { NgForm } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { RegistrationFormComponent } from '../../registration- 
form/registration-form.component';

@Component({
selector: 'app-edit-user-detail',
templateUrl: './edit-user-detail.component.html',
styleUrls: ['./edit-user-detail.component.css']
})

export class EditUserDetailComponent implements OnInit , AfterViewInit {
 public editThisDetail: userRegistrationData;
value : string;
constructor(private httpClient: HttpClient, ){}

@ViewChild('RegistrationFormComponent') registrationFormComponent: 
RegistrationFormComponent;


 @ContentChild(RegistrationFormComponent)
public registraionComponent :  RegistrationFormComponent;



  pushData()
  {
    console.log("asif");
    console.log("asif",this.registraionComponent.currentValue);
  }

  ngAfterViewInit()
  {
    this.pushData();
  }

  ngOnInit() {

  }

  }

错误,代码中:RegistrationFormComponent.html:81错误TypeError:无法读取未定义的属性'currentValue'     在EditUserDetailComponent.push ../ src / app / createUser / editUser / edit-user-detail / edit-user-detail.component.ts.EditUserDetailComponent.pushData(edit-user-detail.component.ts:50)处     在EditUserDetailComponent.push ../ src / app / createUser / editUser / edit-user-detail / edit-user-detail.component.ts.EditUserDetailComponent.ngAfterViewInit

1 个答案:

答案 0 :(得分:0)

如果您是SecondComponent的孩子,而EditUserDetailComponent是父母,则可以按以下方式通过DI进行访问-

EditUserDetailComponent

RegistrationFormComponent

删除以下代码

constructor(private httpClient: HttpClient, 
     @Inject(forwardRef(() => RegistrationFormComponent)) private registraionComponent:RegistrationFormComponent){

}