0

I have popular error about Angular 2. When I'm calling function from another component I face this error.

"Cannot read property 'onChange' of undefined at CD019.onChangeParent"

My codes below:

  @ViewChild('testComponent') testComponent: CD020;

onChangeParent() {
    this.testComponent.onChange;

}

and child component is

   @Output() childChanged = new EventEmitter<string>();
    onChange(value: string, test: string) {
        this.childChanged.emit(value);
        this.Test(test);
    }

Thanks for your help already.

4
  • What error(s) do you get? Can you post them in your question? Also, if you are calling a function on your child, you must add parentheses at the end of a function call. this.testComponent.onChange() //added () Commented Jun 1, 2017 at 6:36
  • The error is "Cannot read property 'onChange' of undefined at CD019.onChangeParent". I tried with () and parameters but it doesn't change. Commented Jun 1, 2017 at 6:38
  • Can you also post your html for the parent? And what type is your child component? Is it CD020? Commented Jun 1, 2017 at 6:52
  • Yes, its type is CD020. I posted it, i'm using child component with selector in parent html. but i want to trigger child's function when i'm using parent's method. Commented Jun 1, 2017 at 6:59

2 Answers 2

1

According to the documentation you shall use the component type as the identifier for the ViewChild decorator. https://angular.io/docs/ts/latest/api/core/index/ViewChild-decorator.html

@ViewChild(CD020) testComponent: CD020;
Sign up to request clarification or add additional context in comments.

1 Comment

@canberkdogmus if the answer solved your issue. You should mark it as accepted: meta.stackexchange.com/a/135826
0

You need to make sure @ViewChild(ChildType) testComponent: ChildType uses the same type E.g.your child component:

@Component({
  selector: 'app-my-child',
  templateUrl: './my-child.component.html',
  styleUrls: ['./my-child.component.css']
})
export class CD020 implements OnInit {
    ngOnInit() {
    }
    @Output() childChanged = new EventEmitter<string>();
    onChange(value: string, test: string) {
        this.childChanged.emit(value);
    }
}

your parent html:

<app-my-child></app-my-child> <!-- uses the template selector name -->

your parent code:

//note the name inside the viewChild and the type of testComponent
@ViewChild(CD020) testComponent: CD020;

onChangeParent() {
    // must pass two arguments.
    this.testComponent.onChange("someValue","someTest");

}

2 Comments

If ViewChild is used the way you suggest (with the selector name), wouldn't the field testComponent be an ElementRef or a TemplateRef?
@Hampus, I am sorry, I was writing the wrong answer. You should use the type of the component inside the @ViewChild() as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.