admin管理员组文章数量:1415684
code example below:
<div class="form-floating mb-3" data-controller="home">
<input type="text"
class="form-control autoplete-input"
id="city" placeholder="Vila Mariana"
data-home-target="district"
data-action="keyup->home#autoComplete" />
<label for="city">Bairro</label>
<div class="autoplete-body">
<ul>
{#for state in states}
<li data-home-states-value="{state.uf}"
data-home-name-value="{state.name}"
data-action="click->home#selectItem">
{state.name}
</li>
{/for}
</ul>
</div>
</div>
The ul:li
element is already inside a data-controller=home
scope
but it fails to capture the value of li.
It only works if I declare:
{#for state in states}
<li
data-controller="home"
data-home-states-value="{state.uf}"
data-home-name-value="{state.name}"
data-action="click->home#selectItem">
{state.name}
</li>
{/for}
code example below:
<div class="form-floating mb-3" data-controller="home">
<input type="text"
class="form-control autoplete-input"
id="city" placeholder="Vila Mariana"
data-home-target="district"
data-action="keyup->home#autoComplete" />
<label for="city">Bairro</label>
<div class="autoplete-body">
<ul>
{#for state in states}
<li data-home-states-value="{state.uf}"
data-home-name-value="{state.name}"
data-action="click->home#selectItem">
{state.name}
</li>
{/for}
</ul>
</div>
</div>
The ul:li
element is already inside a data-controller=home
scope
but it fails to capture the value of li.
It only works if I declare:
{#for state in states}
<li
data-controller="home"
data-home-states-value="{state.uf}"
data-home-name-value="{state.name}"
data-action="click->home#selectItem">
{state.name}
</li>
{/for}
Share
Improve this question
asked Aug 1, 2021 at 20:29
JoseVieiraDevJoseVieiraDev
511 silver badge4 bronze badges
2 Answers
Reset to default 9I believe by design is must be in the same element as the controller definition.
Looking at the values documentation, It is purposefully put in the same element as the controller definition.
Where as the in the targets documentation, the targets are put in child elements.
I was having the same issue as you; the above is the conclusion I came to after way too much googling, and messing around with my own code. Though, I was unable to find a concrete definition anywhere on if this is expected behavior.
If you want to access that data you always define a target, or multiple, and access the data on that.
Setting in html:
data-home-target="abc"
Assign in controller:
static targets = ["abc"]
Access in controller:
this.abcTarget.dataset['dataname']
It looks like you might want to be accessing the data on an event click?
You could also do this in the event handler:
event.target.dataset['dataname']
本文标签: javascriptValues stimulus js don39t working a single escope of a controllerStack Overflow
版权声明:本文标题:javascript - Values stimulus js don't working a single escope of a controller - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745200406a2647341.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论