最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular ng-click for drodpdown option tag - Stack Overflow

programmeradmin2浏览0评论

Here is a jsfiddle. /

I have a dropdown selection here for 2. names and numbers.

Select name and then select numbers. once a dropdown is selected for numbers, execute function get executed and a output is displayed at the console.

This might look like it is working fine. but open the console and click on the dropdown. execute function executes before we even select a dropdown option.

How to ensure that execute function should execute only when the user clicks on one of the option tags ?

Markup:

<div ng-controller="MyCtrl">
    <div>
        <label>Names:</label>
        <select ng-model="params.name">
            <option value="pa">Taeo</option>
            <option value="ws">Wers</option>
            <option value="pn">Petin</option>
        </select>
        <br>
        <label>Numbers:</label>
        <select ng-click="execute()">
            <option value="22">22</option>
            <option value="33">33</option>
        </select>
    </div>
</div>

Here is a jsfiddle. http://jsfiddle/cuycbxxp/

I have a dropdown selection here for 2. names and numbers.

Select name and then select numbers. once a dropdown is selected for numbers, execute function get executed and a output is displayed at the console.

This might look like it is working fine. but open the console and click on the dropdown. execute function executes before we even select a dropdown option.

How to ensure that execute function should execute only when the user clicks on one of the option tags ?

Markup:

<div ng-controller="MyCtrl">
    <div>
        <label>Names:</label>
        <select ng-model="params.name">
            <option value="pa">Taeo</option>
            <option value="ws">Wers</option>
            <option value="pn">Petin</option>
        </select>
        <br>
        <label>Numbers:</label>
        <select ng-click="execute()">
            <option value="22">22</option>
            <option value="33">33</option>
        </select>
    </div>
</div>
Share Improve this question edited May 3, 2016 at 23:28 angelcool 2,5461 gold badge25 silver badges26 bronze badges asked May 3, 2016 at 22:04 PatrickPatrick 4,3086 gold badges21 silver badges33 bronze badges 3
  • 1 ng-click will trigger when you click any element with that decorator, not the option. I think ng-change will probably be a better directive in your case ? – thsorens Commented May 3, 2016 at 22:07
  • @thsorens- wld u like to put it as an answer ? i see others have answered it but u did it first. Thanks guys. my understanding is a tad better now with ng-click and ng-change. – Patrick Commented May 3, 2016 at 22:10
  • 1 I posted my answer now. – thsorens Commented May 3, 2016 at 22:14
Add a ment  | 

3 Answers 3

Reset to default 3

ng-click will trigger when you click any element with that decorator, not the option. I think ng-change will probably be a better directive in your case.

Change <select ng-click="execute()"> to <select ng-change="execute()">

The ng-click gets executed whenever you click on the object (in this case, the dropdown). The ng-change gets executed when the form element is changed, which is whenever the user changes the dropdown item.

You need to use angular ng-change (ngChange).

Use

ng-change="execute()"

instead of

ng-click="execute()"

Doc Reference ng-change: https://docs.angularjs/api/ng/directive/ngChange

Doc Reference ng-click: https://docs.angularjs/api/ngTouch/directive/ngClick

Since you use ng-click, the click event fires at the time you click the select box. However if you use ng-change, the change event will fire when the select box's value change based on the option selection, thus execute() will fire.

发布评论

评论列表(0)

  1. 暂无评论