javascript - Values ​stimulus js don't working a single escope of a controller - Stack Overflow

code example below:<div class="form-floating mb-3" data-controller="home">&l

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
Add a ment  | 

2 Answers 2

Reset to default 9

I 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']

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745200406a4616294.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信