Search code examples
angularjsbootstrap-accordion

Use my own functions in a template for uib-accordion in angularJS application


I have an AngularJS application with an bootstrap accordion. But I want to use the accordion with my own template and use my own functions in this one.

So, this is my html code:

<div ng-controller="AccordionDemoCtrl">
  <script type="text/ng-template" id="group-template.html">
    <div class="panel-heading">
    <h4 class="panel-title" style="color:#fa39c3">
     <a href tabindex="0" class="accordion-toggle" ng-click="toggleOpen()" uib-accordion-transclude="heading">
      <span uib-accordion-header ng-class="{'text-muted': isDisabled}">
        {{heading}}
      </span>
     </a>
    </h4>
</div>
<div class="panel-collapse collapse" uib-collapse="!isOpen">
  <div class="panel-body" style="text-align: right" ng-transclude></div>
</div>
</script>
 <uib-accordion close-others="oneAtATime">
  <div uib-accordion-group class="panel-default" heading="Custom template" template-url="group-template.html">
    Hello
  </div>
</uib-accordion>
</div>

This is a plunkr of the code I use here

All of this works, but I want to use some functions from my controller in the script/template part.

Do you know how I can do this?


Solution

  • The thing is you are in a child Scope here - created by UI-Bootstrap. If you really want to do that - the only way is to access it via $parent

    the following should work

    <!doctype html>
    <html ng-app="ui.bootstrap.demo">
      <head>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-animate.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-sanitize.js"></script>
        <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.js"></script>
        <script src="example.js"></script>
        <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
      </head>
      <body>
    
    <div ng-controller="AccordionDemoCtrl">
      <script type="text/ng-template" id="group-template.html">
        <div class="panel-heading">
          <h4 class="panel-title" style="color:#fa39c3">
            <a href tabindex="0" class="accordion-toggle" ng-click="$parent.showAlert(); toggleOpen();" uib-accordion-transclude="heading">
              <span uib-accordion-header ng-class="{'text-muted': isDisabled}">
                {{heading}}
              </span>
            </a>
          </h4>
        </div>
        <div class="panel-collapse collapse" uib-collapse="!isOpen">
          <div class="panel-body" style="text-align: right" ng-transclude></div>
        </div>
      </script>
    
      <uib-accordion close-others="oneAtATime">
        <div uib-accordion-group class="panel-default" heading="Custom template" template-url="group-template.html">
          Hello
        </div>
      </uib-accordion>
    </div>
      </body>
    </html>
    

    And your JS

    angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
    angular.module('ui.bootstrap.demo').controller('AccordionDemoCtrl', function ($scope) {
      $scope.oneAtATime = true;
    
      $scope.groups = [
        {
          title: 'Dynamic Group Header - 1',
          content: 'Dynamic Group Body - 1'
        },
        {
          title: 'Dynamic Group Header - 2',
          content: 'Dynamic Group Body - 2'
        }
      ];
    
      $scope.items = ['Item 1', 'Item 2', 'Item 3'];
    
      $scope.showAlert = function()
      {
        alert('Hello');
      };
    
      $scope.addItem = function() {
        var newItemNo = $scope.items.length + 1;
        $scope.items.push('Item ' + newItemNo);
      };
    
      $scope.status = {
        isCustomHeaderOpen: false,
        isFirstOpen: true,
        isFirstDisabled: false
      };
    });