Crocsx
Crocsx

Reputation: 7589

1 component called multiple times. same if angularjs

so, this is a problem recurrent in the project, so I don't want to find a clean method, but an effective method to fix it.

I have a componentA and a componentB

componentA got an element with an ID that he want to modify in code.

for some reason, componentA can call componentB, and for other manipulation, you could have componentB calling anothert componentA.

in the webpage, you would have : componentA -> componentB -> componentA again.

now, from the second componentA i want to modify the element with an ID, but since there is already one on the page, it doesn't work.

how can i bind elementA-1 with componentA1 and elementA-2 with componentA-2 without using the id ?

Thanks

Upvotes: 1

Views: 29

Answers (1)

Crocsx
Crocsx

Reputation: 7589

You can give the element a custom ID from within the component. then you can select by id the element from the component

componentA.js

// this is to make a random ID
componentScope.elementID = Math.random().toString(36).substring(7);

componentA.html

id="{{componentScope.elementID}}"

so you can access from the component using

document.getElementById(componentScope.elementID);

Upvotes: 1

Related Questions