0

I am using button to create element in javascript DOM way as follows

      var app = angular.module('app', []);
     function createElement2(resource){
       el= document.createElement('textarea');
       $(el).attr('ID', "Textbox0");
       $(el).attr('type', "text");
       $(el).attr('value', "Textbox0");
       $(el).attr("name","Textbox0");
       $(el).attr("ng-model", "html");
      //this line will be diffrent for element
    document.getElementById("areaT").appendChild(el)
    }
    app.directive('dynamic', function ($compile) {
        return {
            restrict: 'A',
           replace: true,
          link: function (scope, ele, attrs) {
              scope.$watch(attrs.dynamic, function(html) {
               console.log("test")
                 ele.html(html);
            $compile(ele.contents())(scope);
           });
          }
         };
       });

     function MyController($scope) {
     $scope.click = function(arg) {
      alert('Clicked ' + arg);
  }
     $scope.html = '<a ng-click="click(1)" href="#">Click me</a>';
    }

The problem is I want to a tag customize directive Angluarjs to any element create with different validtion and the dirctive code execute only when the page load.In another words I want to execute the next piece of code every time I create element

<!DOCTYPE html>
 <html ng-app="app">

 <head>
  <script data-require="angular.js@1.0.7" data-semver="1.0.7"     src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    </head>

 <body>
<h1>Compile dynamic HTML</h1>
<div id="areaT" ng-controller="MyController">
  <textarea ng-model="html"></textarea>
  <div dynamic="html"></div>
  </div>
  <input type="button" onclick="createElement2(this)">
  </body>

   </html>

note that the textarea in the html code when enter any code the console.log("test") and so on will be execute. what I need when click on button the textarea that will be adding execute what the textarea in the html code execute (console.log("test") and so) when enter text inside it

myomyo
  • 51
  • 1
  • 9
  • Possible duplicate of [AngularJS - Compiling dynamic HTML strings from database](http://stackoverflow.com/questions/18157305/angularjs-compiling-dynamic-html-strings-from-database) – Vikas Bansal Oct 12 '16 at 07:13
  • @VikasBansal thank you; Editied code please if you can apply it .Note that the textarea in the html code when enter any code the console.log("test") and so on will be execute. what I need when click on button the textarea that will be adding execute what the textarea in the html code execute (console.log("test") and so on) when enter text inside it – myomyo Oct 12 '16 at 09:29

0 Answers0