2

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>

5 Answers 5

2

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

Try like this:

@ViewChild('draganddropimage', { static: true }) dragDropImageRef: ElementRef;
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

@ViewChild('draganddropimage') dragDropImageRef: ElementRef;

1 Comment

Hi ilyas thanks for the reply but still not working getting error TS2554: Expected 2 arguments, but got 1.
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() {}
    }
}

3 Comments

Hi @Mustapha thanks alot for reply but still its not working error im getting is error TS2554: Expected 2 arguments, but got 1.
I edited the answer. you must put the name of the viewChild as string @ViewChild('draganddropimage')
If you're using Angular 8 you need to add { static: true } Check this post stackoverflow.com/questions/56473899/…
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() {}

Comments

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.