1

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>
Bill P
  • 3,622
  • 10
  • 20
  • 32
Yasir Mehmood
  • 57
  • 1
  • 10

5 Answers5

2

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

Try like this:

@ViewChild('draganddropimage', { static: true }) dragDropImageRef: ElementRef;
Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
0

You need to declare a variable and add the decorator to it :

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')
    private draganddropimage: ElementRef; 

    constructor() {}

    ngOnInit() {}
    }
}
Mustapha Larhrouch
  • 3,373
  • 3
  • 14
  • 28
  • Hi @Mustapha thanks alot for reply but still its not working error im getting is **error TS2554: Expected 2 arguments, but got 1.** – Yasir Mehmood Sep 05 '19 at 08:18
  • I edited the answer. you must put the name of the viewChild as string `@ViewChild('draganddropimage')` – Mustapha Larhrouch Sep 05 '19 at 08:27
  • If you're using Angular 8 you need to add { static: true } Check this post https://stackoverflow.com/questions/56473899/error-ts2554-expected-2-arguments-but-got-1-with-viewchild – ilyas shabi Sep 05 '19 at 08:28
0

I think you decorate constructor, try to set a name for @ViewChild(draganddropimage) draganddropimage

Carnaru Valentin
  • 1,690
  • 17
  • 27
0

You need to import ElementRef and then use this snippet to get your template reference using ViewChild :

@ViewChild('draganddropimage') dragDropImageRef: ElementRef;
ilyas shabi
  • 194
  • 1
  • 7
0

Just fighting with the same problem. You need to put something between the decorator and the constructor:

export class SomeComponent implements OnInit {
  @ViewChild(SomeChildComponent, { static: true })

  someVariable: SomeChildComponent; // this row is the magic (otherwise you decorate the constructor)

  constructor() {}
AlexV
  • 249
  • 3
  • 8