HiveBrain v1.2.0
Get Started
← Back to all entries
debugtypescriptangularCritical

Angular @ViewChild() error: Expected 2 arguments, but got 1

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
angularerrorgotexpectedviewchildbutarguments

Problem

When trying ViewChild I am getting the error. Error is "An argument for 'opts' was not provided."

Both @ViewChild is giving the error.

import { Component, OnInit, ElementRef, ViewChild, Output, EventEmitter } from '@angular/core';
import { Ingredient } from 'src/app/shared/ingredient.model';

@Component({
selector: 'app-shopping-edit',
templateUrl: './shopping-edit.component.html',
styleUrls: ['./shopping-edit.component.css']
})
export class ShoppingEditComponent implements OnInit {

@ViewChild('nameInput') nameInputRef: ElementRef;
@ViewChild('amountInput') amountInputRef: ElementRef;
@Output() ingredientAdded = new EventEmitter();
constructor() {}

ngOnInit() {
}

onAddItem() {
const ingName = this.nameInputRef.nativeElement.value;
const ingAmount = this.amountInputRef.nativeElement.value;
const newIngredient = new Ingredient(ingName, ingAmount);
this.ingredientAdded.emit(newIngredient);
}

}



ts(11,2): error TS2554: Expected 2 arguments, but got 1.

Solution

In Angular 8 , ViewChild takes 2 parameters

@ViewChild(ChildDirective, {static: false}) Component

Code Snippets

@ViewChild(ChildDirective, {static: false}) Component

Context

Stack Overflow Q#56704164, score: 626

Revisions (0)

No revisions yet.