Handle open/collapse events of Accordion in Angular

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

If I have this code:


      {{group.content}}

         


        
7条回答
  •  無奈伤痛
    2020-11-29 02:29

    Here's a solution based on pkozlowski.opensource solution.
    Instead of adding a $watch on each item of the collection, you can use a dynamically defined Property. Here, you can bind the IsOpened property of the group to the is-open attribute.

    
       {{group.content}}
    
    

    So, you can dynamically add the IsOpened property on each item of the collection in the controller :

    $scope.groups.forEach(function(item) {
      var isOpened = false;
      Object.defineProperty(item, "IsOpened", {
        get: function() {
          return isOpened;
        },
        set: function(newValue) {
          isOpened = newValue;
          if (isOpened) {
            console.log(item); // do something...
          }
        }
      });
    });
    

    Using properties instead of watches is better for performances.

提交回复
热议问题