11

I have a component whos template looks like:

..blabla
 <img md-card-image src="..">
..blabla

How do I obtain a reference inside my component.ts so that I could change it's src using angular 4?

Jue Debutat
  • 367
  • 1
  • 3
  • 12
  • 3
    You don't. Instead, you use [src]="someDynamicExpression", and whe the dynamic expression value changes, the src changes automatically. This is called "binding", and is at the core of angular. – JB Nizet May 12 '17 at 17:03
  • You can get a reference with a [Template reference variable](https://angular.io/docs/ts/latest/guide/template-syntax.html#!#ref-vars), but as @JBNizet said, it's better to use binding for that task – Omri Luzon May 12 '17 at 17:07

1 Answers1

31

in HTML:

<img #img md-card-image src="..">

in TS:

@ViewChild('img') img: ElementRef;
Julia Passynkova
  • 17,256
  • 6
  • 33
  • 32