Search code examples
angularangular-components

Facing issue while calling @viewchild in angular


Hi guys i'm working with angular and want to call @viewChild in ts file but facing an error "Decorators are not valid here." cant find any specific solution for my problem

I have tried many links regarding this problem including angular documentation, stackoverflow, youtube but not able to find any appropriate solution to my problem

import { Component, OnInit, ViewChild } from '@angular/core';
import * as $ from 'jquery';

@Component({
    selector: 'app-upload-card-details',
    templateUrl: './upload-card-details.component.html',
    styleUrls: ['./upload-card-details.component.css']
})
export class UploadCardDetailsComponent implements OnInit {
    @ViewChild(draganddropimage)
    constructor() {}

ngOnInit() {}
    }

HTML:Code
<div class="modal fade custom-modal-setting" id="drag-and-drop-image" #draganddropimage tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"></div>

Solution

  • From Angular 8, you need to add { static: true } in ViewChild

    Try like this:

    @ViewChild('draganddropimage', { static: true }) dragDropImageRef: ElementRef;