hotwired / stimulus

A modest JavaScript framework for the HTML you already have
https://stimulus.hotwired.dev/
MIT License
12.67k stars 420 forks source link

Cannot find target when controller mounted on <html> #152

Closed ngan closed 6 years ago

ngan commented 6 years ago

I have the following scenario:

<html data-controller="foo">
  <body>
    <div data-target="foo.bar"></div>
  </body>
</html>
// foo_controller.js
import { Controller } from 'stimulus';

export default class extends Controller {
  static targets = [
    "bar"
  ];
}

I'll get the following error: Error connecting controller...Missing target element "foo.bar"

If I move the controller to the <body> element, then it'll work just fine. I believe <html> controllers should be fully supported: https://discourse.stimulusjs.org/t/controllers-on-html-element/261/2

javan commented 6 years ago

I suspect this is the same issue as https://github.com/stimulusjs/stimulus/issues/97. Try one of the suggestions in https://github.com/stimulusjs/stimulus/issues/97#issuecomment-363874711 and it should work correctly.

Stimulus 1.1 (unreleased) already has a fix in place: https://github.com/stimulusjs/stimulus/pull/131