Handle open/collapse events of Accordion in Angular

后端 未结 7 1392
猫巷女王i
猫巷女王i 2020-11-29 02:04

If I have this code:


      {{group.content}}



        
相关标签:
7条回答
  • 2020-11-29 02:42

    You can do it w/ an Angular directive:

    html

    <div uib-accordion-group is-open="property.display_detail" ng-repeat="property in properties">
      <div uib-accordion-heading ng-click="property.display_detail = ! property.display_detail">
        some heading text
      </div>
      <!-- here is the accordion body -->
      <div ng-init="i=$index">  <!-- I keep track of the index of ng-repeat -->
        <!-- and I call a custom directive -->
        <mydirective mydirective_model="properties" mydirective_index="{% verbatim ng %}{{ i }}{% endverbatim ng %}">
          here is the body
        </mydirective>
      </div>
    </div>
    

    js

    app.directive("mydirective", function() {
      return {
        restrict: "EAC",  
        link: function(scope, element, attrs) {
          /* note that ng converts everything to camelCase */
          var model = attrs["mydirectiveModel"];
          var index = attrs["mydirectiveIndex"];
          var watched_name = model + "[" + index + "].display_detail"
          scope.$watch(watched_name, function(is_displayed) {
            if (is_displayed) {
              alert("you opened something");
            }
            else {
              alert("you closed something");
            }
          });
        }
      }
    });
    

    There are some idiosyncrasies about my setup there (I use Django, hence the "{% verbatim %}" tags), but the method should work.

    0 讨论(0)
提交回复
热议问题