Stimulus Discourse

New controller instance created via javascript


#1

If a new stimulus controller instance is added to the page via javascript, how do I get it initialized?


#2

Controllers are automatically initialized regardless of how/when they’re added to the page:

Stimulus works by continuously monitoring the page, waiting for the magic data-controller attribute to appear.

https://stimulusjs.org/handbook/introduction#connecting-html-to-javascript


#3

Thank you, I’m not sure what the issue was before, but it is working as expected.

What about dynamically added targets? Those don’t appear to be picked up stimulus automatically.


#4

Yes they are as long as they are inside the controller element and scoped correctly with the controller name