AngularJS Nested Directives In an effort to learn more about AngularJS directives, I decided to create a simple, edit-in-place directive. I built a simple directive that would display a value, along with an edit link and the edit link would toggle visibility of an input and the save/cancel links. From there, I wanted a setup where I could make multiple properties of an object editable, and I wanted to share the link controls and trigger updates to each property together.

I decided to create a wrapper directive to handle the links and ended up with a working version in which I ended up broadcasting events from the parent(wrapper) directive and taking action in the nested directives based on that. I feel like there is probably a better approach, but this is what I was able to make work.

The HTML:

&lt;html ng-app="editInPlaceApp"&gt;
&lt;head&gt;
    &lt;script data-require="angular.js@*" data-semver="1.3.6" src="https://code.angularjs.org/1.3.6/angular.js"&gt;&lt;/script&gt;
    &lt;link rel="stylesheet" href="style.css" /&gt;

  &lt;/head&gt;

  &lt;body&gt;
    &lt;div ng-controller="MainController"&gt;
      &lt;ul&gt;
        &lt;li ng-repeat="p in people"&gt;
          &lt;wrap-in-place&gt;
            &lt;value-in-place value="p.fname"&gt;&lt;/value-in-place&gt;
            &lt;value-in-place value="p.lname"&gt;&lt;/value-in-place&gt;
          &lt;/wrap-in-place&gt;
        &lt;/li&gt;
      &lt;/ul&gt;
    &lt;/div&gt;

    &lt;script src="script.js"&gt;&lt;/script&gt;
  &lt;/body&gt;

&lt;/html&gt;


The JavaScript:

  var app = angular.module('editInPlaceApp',['myDirectives']);
  app.controller('MainController', ['$scope', 'PersonService', function($scope, PersonService){
    $scope.people = PersonService.loadPeople();
  }])
  .factory('PersonService', function(){
    var factory = {};

    factory.loadPeople = function(){
      var peeps = [{"id":"abc0", fname:"John", lname:"Doe"},
        {"id":"abc1", fname:"Jane", lname:"Smith"},
        {"id":"abc2", fname:"Bob", lname:"Jones"}];

      return peeps;
    };

    return factory;
  });

  angular.module('myDirectives',[])
  .directive('wrapInPlace', function(){
    return {
      restrict: 'E',
      transclude: true,
      scope:true,
      template: '&lt;span ng-transclude&gt;&lt;/span&gt; &lt;a href="#" ng-click="edit()" ng-show="!editChildren"&gt;edit&lt;/a&gt; &lt;span ng-show="editChildren"&gt;&lt;a href="#" ng-click="saveChildren()"&gt;save&lt;/a&gt; &lt;a href="#" ng-click="cancel()"&gt;cancel&lt;/a&gt;&lt;/span&gt;',
      link: function($scope, element, attrs){
        $scope.edit = function(){
          $scope.editChildren = true;
          $scope.$broadcast('editChildren',{"val":$scope.editChildren});
        };

        $scope.cancel = function(){
          $scope.editChildren = false;
          $scope.$broadcast('editChildren',{"val":$scope.editChildren});
        };

        $scope.saveChildren = function(){
          $scope.editChildren = false;
          $scope.$broadcast('saveChildren');
        };
      }
    };
  })
  .directive('valueInPlace', function(){
    return {
      restrict: 'E',
      scope: {
        value: '='
      },
      template: '&lt;span ng-show="!editing"&gt;{{value}}&lt;/span&gt;&lt;input type="text" ng-model="updatedValue" ng-show="editing"&gt;',
      link: function ( $scope, element, attrs ) {
        $scope.editing = false;
        $scope.updatedValue = $scope.value;

        $scope.$on('editChildren', function(event, args){
          $scope.editing = args.val;
          if(!args.val){
            $scope.updatedValue = $scope.value;
          }
        });

        $scope.$on('saveChildren', function(event, args){
          $scope.editing = false;
          $scope.value = $scope.updatedValue;
        });

      }
    };
  });


The full working example can be found here.
It is always better to follow component approach in angularjs. Even if your using angularjs prior to version 1.5.x you can use directives to emulate this approach thus making each directive completely isolated manageable code chunk. it is always better to avoid usage of $scope. it leads to creation of scope soup. To avoid this use controllerAs namespace. and rather than broadcasting events which might lead to uncertain behavious use services to enable communication between directives. This will make your code more manageable and readable and in the future you could easily port to angular2 if you want.