Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS 1.6 -Accessing elements in the component's template

How can I access an element in my component template from my js code?
Suppose I have many components with this template:

<div style="width: inherit; height: inherit;">
    <img class="elem"> </img>
</div>

and I want each component's controller to access its own img - how can I do it?

like image 526
Idov Avatar asked Sep 09 '26 01:09

Idov


1 Answers

You can inject $element into your component controller:

componetnController($element) {
    let img = $element.find('img');
}
like image 173
Bartek Fryzowicz Avatar answered Sep 11 '26 14:09

Bartek Fryzowicz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!