8

I'm still pretty new to angular, so this may be a basic question, but I haven't been able to figure it out yet.

How do I put a checkbox (bound to a property of my model) into a header for an angular-ui accordion? I can get the checkbox in there, but the click never seems to get to it, i.e., it never gets "checked".

<accordion>
  <accordion-group ng-repeat="group in groups" is-open="group.open">
    <accordion-heading>
      <input 
          type="checkbox" 
          ng-model="group.checked" 
          ng-click="checkboxClick(group, $event)" />
      Title
    </accordion-heading>
    {{group.content}}
  </accordion-group>    
</accordion>

According to this jQueryUI accordion with checkboxes, I can get something like the desired behavior with the standard jquery-ui accordion by calling e.preventDefault() on the click event, but that doesn't seem to work with the angular-ui accordion directive.

See this plunker here: http://plnkr.co/edit/QMnaXJeMagrTPRjbIZbL?p=preview.

3 Answers 3

11
<accordion>
  <accordion-group ng-repeat="group in groups" is-open="group.open">
    <accordion-heading>
      <input type="checkbox" ng-checked="group.checked" ng-click="checkboxClick(group, $event)" />
      {{group.title}}
    </accordion-heading>
    {{group.content}}
  </accordion-group>    

And:

$scope.checkboxClick = function(group, $event){
  $event.stopPropagation();
}

Example: Plunker

Sign up to request clarification or add additional context in comments.

3 Comments

That doesn't seem to work - the checkbox never changes state. See plnkr.co/edit/tTGy9grHIByb8THbSz9W?p=preview.
My fault, I meant ng-checked. You are also going to need a timeout in your click event. You can use $timeout for this. I edited my answer and gave you an example.
OK, that was it. A few minor tweaks and I got it working how I wanted. See plnkr.co/edit/O2p0Dj9RZvpXDlao6RLd?p=preview. Many thanks!
0

In the checkboxClick(group, $event) method change e.preventDefault(); to $event.preventDefault();.

See also Betty St's comment on this answer.

Comments

0

Add

ng-click="$event.stopPropagation();"

like given Below

<uib-accordion>
    <uib-accordion-group is-open="status.open">
        <uib-accordion-heading>
         <input id="Checkbox1" ng-click="$event.stopPropagation();" type="checkbox" /> I can have markup, too! <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': status.open, 'glyphicon-chevron-right': !status.open}"></i>
      </uib-accordion-heading>
  </uib-accordion>

Comments

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.