1

In this plunk I have an Angular UI dropdown. Is there a way NOT to repeat the ng-click in each <a> element?

HTML

<div class="btn-group" uib-dropdown>
  <button id="btn-append-to-body" type="button" class="btn btn-primary" 
      uib-dropdown-toggle>
      {{selection}} <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" uib-dropdown-menu="" role="menu" 
        aria-labelledby="btn-append-to-body">
    <li role="menuitem">
      <a href="#" ng-click="selectItem('1')">The first item</a>
    </li>
    <li role="menuitem">
      <a href="#" ng-click="selectItem('2')">Another item</a>
    </li>
    <li role="menuitem">
      <a href="#" ng-click="selectItem('3')">Yet another item</a>
    </li>
 </ul>

3
  • Delegate event to the UL or better create simple directive to make this delegation for you. Commented May 6, 2016 at 13:05
  • can you point me to an explanation of "delegate event" ? Commented May 6, 2016 at 13:11
  • Just normal DOM event delegation. Nothing angular specific. But it's better to create directive for this anyway (unless you use jQuery in the project, which already has it). Commented May 6, 2016 at 13:12

2 Answers 2

2

this plunker shows how to use a global ng-click (passing the source $event) and selecting the data-value attribute as selected value :

<ul class="dropdown-menu" ng-click="selectItem($event)" uib-dropdown-menu="" role="menu" aria-labelledby="btn-append-to-body">
           <li role="menuitem">
              <a href="#" data-value="1" >The first item</a>
            </li>
            <li role="menuitem">
              <a href="#" data-value="2">Another item</a>
            </li>
            <li role="menuitem">
              <a href="#" data-value="3">Yet another item</a>
            </li>
        </ul>

The selectItem() function , getting the data-value property of selected anchor (ev.target):

$scope.selectItem = function(ev) {
    $scope.selection = ev.target.dataset.value
};
Sign up to request clarification or add additional context in comments.

5 Comments

Perfect, is there a way to set the label selected on the button, instead of the code?
No, it's not reliable (will not work for <a href="#" data-value="3">Yet another item <i class="fa fa-star"></i></a> if you click icon).
but I don't need the icon, just the label
Sure it will work for this structure. Not reliable means that it can break even if CSS changes (maring, for example). You also can't change HTML, like adding anything inside labels. If it's okay with you, then fine. Just be aware about limitations.
To set the value using the text of the link you can use $scope.selection = ev.target.textContent (plnkr.co/edit/7XW5eduSTreCkNLfimbC?p=preview)
0

If I understood you correctly you can write like below with ng-repeat

Controller Code:

//Your controller logic
$scope.selectItems = ["First Item", "Another Item", "One More Item"]
//some more logic in your controller

HTML View

<ul class="dropdown-menu" uib-dropdown-menu="" role="menu" aria-labelledby="btn-append-to-body" ng-repeat="(key, value) in selectItems ">
      <li role="menuitem">
        <a href="#" ng-click="selectItem('key')">{{value}}</a>
      </li>
    </ul>

as I don't have access I can't able to update your plunker

7 Comments

No, I cannot use ng-repeat in this example. I updated the question to be clearer.
because I'm trying to keep the dropdown options in the HTML markup, not in the javascript
What do you mean by keep the dropdown options in the HTML markup, not in the javascript
How you will pass data from one element to another element with out using javascript?
I will use javascript to get the selected element, but the list of options I prefer to mantain in the HTML instead of an array in the javascript
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.