title: adding dynamic content to a dynamically created content in angular js
body: <p>I want to showcase the shopping cart category like a tree structure.  When I click on the parent <code>ul&gt;li</code> element it gets data from the server and dynamically adds the new set of data.  I have n levels, initially when I click on the first level I create a dynamic children set and put it under the parent using its class name.  The same function is called for the newly created child, but this time its updating the super parent of the element clicked.  I have created a  <a href="http://plnkr.co/edit/D7vZgJMuCcQelNfYstvp?p=preview" rel="nofollow">plunker</a> for a detailed explanation.</p>
  
  <p>When I click on the men category, its child elements are populated with:</p>
  
  <ul>
  <li>wrist watch for men</li>
  <li>shirt</li>
  </ul>
  
  <p>When I click on "wrist watch for men", it should add the next level below it - but its over writing the existing data. </p>
  
  <p>Please help me.  I am new to angular.</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html ng-app="plunker"&gt;
  
  &lt;head&gt;
    &lt;meta charset="utf-8" /&gt;
    &lt;title&gt;AngularJS Plunker&lt;/title&gt;
    &lt;script&gt;
      document.write('&lt;base href="' + document.location + '" /&gt;');
    &lt;/script&gt;
    &lt;link rel="stylesheet" href="style.css" /&gt;
    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"&gt;&lt;/script&gt;
    &lt;script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.3/angular.js"&gt;&lt;/script&gt;
    &lt;script src="app.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body ng-controller="MainCtrl"&gt;
    &lt;ul class="navigation"&gt;
      &lt;li ng-repeat='category in fullTree.category[0]' ng-class='category.category_id' ng-click='dropmenu(category.category_id,category.level,category.has_child_category,$event);event.stopImmediatePropagation()'&gt;
        &lt;p&gt;{{category.category_name}} &lt;i class="ion-chevron-down"&gt;&lt;/i&gt;
        &lt;/p&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>script:</p>
  
  <pre><code>var app = angular.module('plunker', []);
  
  app.controller('MainCtrl', function($scope, $sce, $compile) {
    $scope.name = 'World';
    $scope.html = "";
  
    $scope.htmlElement = function() {
      var html = "&lt;input type='text' ng-model='html'&gt;&lt;/input&gt;";
      return $sce.trustAsHtml(html);
    }
  
    $scope.fullTree = {
      "result": "true",
      "success": 1,
      "category": [
        [{
          "category_id": "prdcat01",
          "category_name": "Men",
          "level": "1",
          "higher_level_catg_no": "0",
          "total_level": "3",
          "has_child_category": "true",
          "total_product": "0"
        }, {
          "category_id": "prdcat02",
          "category_name": "Women",
          "level": "1",
          "higher_level_catg_no": "0",
          "total_level": "3",
          "has_child_category": "true",
          "total_product": "0"
        }, {
          "category_id": "prdcat03",
          "category_name": "Kids",
          "level": "1",
          "higher_level_catg_no": "0",
          "total_level": "1",
          "has_child_category": "false",
          "total_product": "0"
        }],
        [{
          "category_id": "cat01c01",
          "category_name": "Wrist Watch For Men",
          "level": "2",
          "higher_level_catg_no": "prdcat01",
          "has_child_category": "true",
          "total_product": "1"
        }, {
          "category_id": "cat02c01",
          "category_name": "Wrist Watch For Women",
          "level": "2",
          "higher_level_catg_no": "prdcat02",
          "has_child_category": "true",
          "total_product": "0"
        }, {
          "category_id": "cat01c02",
          "category_name": "Shirt for men",
          "level": "2",
          "higher_level_catg_no": "prdcat01",
          "has_child_category": "false",
          "total_product": "0"
        }, {
          "category_id": "cat02c02",
          "category_name": "Shirt For Women",
          "level": "2",
          "higher_level_catg_no": "prdcat02",
          "has_child_category": "false",
          "total_product": "0"
        }],
        [{
          "category_id": "cat0101c01",
          "category_name": "Fastrack Watch For Men",
          "level": "3",
          "higher_level_catg_no": "cat01c01",
          "has_child_category": "false",
          "total_product": "0"
        }, {
          "category_id": "cat0201c01",
          "category_name": "Fastrack Watch For Women",
          "level": "3",
          "higher_level_catg_no": "cat02c01",
          "has_child_category": "false",
          "total_product": "1"
        }, {
          "category_id": "cat0101c02",
          "category_name": "Casio watch for men",
          "level": "3",
          "higher_level_catg_no": "cat01c01",
          "has_child_category": "false",
          "total_product": "1"
        }]
      ],
      "msg": "Data retrieved successfuly"
    };
  
    $scope.dropmenu = function(category_id, level, has_child_category, $event) {
      $event.stopPropagation();
  
      $scope.dummy = [];
  
      for (var i = 0; i &lt; $scope.fullTree.category[level].length; i++) {
        if ($scope.fullTree.category[level][i].higher_level_catg_no == category_id) {
          $scope.dummy.push($scope.fullTree.category[level][i]);
        }
      }
  
      alert(JSON.stringify($scope.dummy))
  
      var fieldHtml = "&lt;ul class='navigation'&gt;&lt;li ng-class={{category.category_id}} ng-repeat='category in dummy' ng-click='dropmenu(category.category_id,category.level,category.has_child_category,$event);event.stopPropagation()'&gt;&lt;p&gt;{{category.category_name}} &lt;i class='ion-chevron-down'&gt;&lt;/i&gt;&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;";
  
      var compiledElement = $compile(fieldHtml)($scope);
  
      $($event.target).closest("." + category_id).append(compiledElement);
    }
  });
  </code></pre>