Andy Andy - 1 year ago 215
AngularJS Question

Passing value out of uib-accordion scope to the parent scope

I am using the uib-accordion directive from ui-boostrap for AngularJS.
I have a question about passing value out of the transcluded directive, uib-accordion-group.

When simply setting a variable to ng-model inside the accordion, it will be attached to the accordion scope, rather than the parent main scope, though it looks like it is in the main scope due to the transclude directive.

In order to pass the value inside the accordion out to the main scope, I need to do something like

which seems wrong.

Is there a way to do it gracefully?

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('AccordionDemoCtrl', function($scope) {


<!doctype html>
<html ng-app="ui.bootstrap.demo">

<script src="//"></script>
<script src="//"></script>
<script src="//"></script>
<script src="example.js"></script>
<link href="//" rel="stylesheet">


<div ng-controller="AccordionDemoCtrl">
<script type="text/ng-template" id="group-template.html">
<div class="panel {{panelClass || 'panel-default'}}">
<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
ng-class="{'text-muted': isDisabled}">{{heading}}</span></a>
<div class="panel-collapse collapse" uib-collapse="!isOpen">
<div class="panel-body" style="text-align: right" ng-transclude></div>

<uib-accordion close-others="oneAtATime">
<uib-accordion-group heading="Static Header, initially expanded" is-open="true">
Simple data model
<input type="text" ng-model="data" />Anti-pattern data2 model
<input type="text" ng-model="$parent.$parent.$parent.data2" />
I read "{{data}}" inside the accordion
I read "{{data2}}" inside the accordion
How do I read "{{data}}" OUTSIDE the accordion
Data2 seems fine "{{data2}}" OUTSIDE the accordion



Answer Source

I had a related issue quite recently, and I ended up modifying the ui-bootstrap-tpls-0.14.3.js file. On line 239 you can see I've added a property called 'model' to the accordion directive's scope object.

scope: {
  heading: '@',               // Interpolate the heading attribute onto this scope
  isOpen: '=?',
  isDisabled: '=?',
  model: '='                   // Custom property added

Then in the controller, I've added an object called item1 to the controller's scope and given it a property called name:

$scope.item1 = {
  name: 'test1'

Lastly, add a 'model' attribute to the accordion group directive and specify item1 as the value to pass into the accordion directive:

<uib-accordion-group model="item1" heading="Static Header, initially expanded" is-open="true">

You should now be able to set the object's property values in the accordion as well as access them outside of the directive.

Here's a plunk with your code modified to work.

I'm not super happy with modifying the UI bootstrap file, but I couldn't come up with a better way to do it. Obviously you'll need to have your own copy of the file stored locally so you can modify it, and I'd suggest adding .custom to the filename to remind you that it's been modified. Probably pop in some comments too so you can migrate any changes you make to a future version of it in case you want to upgrade.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download