Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with angular accordion scope

I am having a directive called templates, the code for templates is like below.

    var templates = function($compile,$parse){
var directive = {
  restrict: 'EA',
  replace: true,
  link: link
};
return directive;

function link(scope, element, attrs) {
 scope.name = "testName";
  var isHtmlCompiled = false;
}
};
 angular.module('templateModules', [])
.directive('templates', templates);

This is mainly used for compiling html code and displaying it.But for the better understanding of the question I am not using it for that purpose in the example. The app.js file is like below

angular.module('ui.bootstrap.demo', ['ui.bootstrap','templateModules']);
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.addItem = function() {
    var newItemNo = $scope.items.length + 1;
    $scope.items.push('Item ' + newItemNo);
  };

 $scope.add = function(){
     alert($scope.name);
 }

  $scope.status = {
    isFirstOpen: true,
    isFirstDisabled: false
  };
});

The index.html is using an accordion like below.

<!doctype html>
<html ng-app="ui.bootstrap.demo">
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.1.js"></script>
    <script src="app.js"></script>
      <script src="template.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

<div ng-controller="AccordionDemoCtrl">

  <accordion close-others="oneAtATime">
    <accordion-group heading="Static Header, initially expanded" is-open="status.isFirstOpen" is-disabled="status.isFirstDisabled">
      This content is straight in the template.
    </accordion-group>
  </accordion>
  <accordion close-others="oneAtATime">
       <accordion-group heading="DYnamic" is-open="status.open" is-disabled="status.isFirstDisabled">
      <div templates="something"></div>
  <button ng-click="add()">Add</button>
    </accordion-group>
  </accordion>

</div>
  </body>
</html>

The problem I am facing is that I am not able to get the value of scope.name from template in the AccordionDemoCtrl. Is there any way to get that value in the AccordionDemoCtrl ?

like image 456
robin Avatar asked Oct 30 '22 23:10

robin


1 Answers

You should be able to access the name property from AccordionDemoCtrl. Because your directive has scope: false and set scope.name in the link function, just like the simplified demo here: JSFiddle.

Here is a working demo modified from your example: Plunker (using two-way binding).


Explanations

The directive accordion doesn't define scope, so its scope is the one from outside controller. transclude:true makes Angular create a child scope for directive template. But since the name on outside controller is a primitive type, setting the value inside template will create a new name on the child scope. See the working demo: JSFiddle. The illustration:

enter image description here

If use an object instead of a primitive, it is working (JSFiddle).

For more details: Understanding Scopes


It seems better to use two-way binding to communicate between directive and outside controller.

Here is a working demo: Plunker

like image 57
Joy Avatar answered Nov 11 '22 04:11

Joy