Gary
Gary

Reputation: 2339

Unable to compile ng-repeat from template in directive

I am trying to add form elements dynamically using JS and will need a directive. I am able to add form elements but when I have ng-options or ng-repeat it does not get compiled. I have an example directive I am using for demo.

http://plnkr.co/edit/JOzTWB6tuyilCJ8Rj37Q

<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
<script>
        var app = angular.module('myApp', []);
      app.controller("fCtrl",function($scope){
        $scope.xx = ['x','c','y','z','a'];

      });

      app.directive('datanType', function ($compile) {

              var testTemplate1 = '<h1 ng-repeat="x in xx">Test</h1>';
              var testTemplate2 = '<h1>Test2</h1>';
              var testTemplate3 = '<h1>Test3</h1>';

              var getTemplate = function(contentType){
                  var template = '';

                  switch(contentType){
                      case 'test1':
                          template = testTemplate1;
                          break;
                      case 'test2':
                          template = testTemplate2;
                          break;
                      case 'test3':
                          template = testTemplate3;
                          break;
                  }

                  return template;
              }; 

              var linker = function(scope, element, attrs){
                element.html(getTemplate(attrs.content));
                $compile(element.contents())(scope);

              };

              return {
                  restrict: "E",
                  replace: true,
                  link: linker,
                  scope: {
                      content:'=',
                      con:'@'
                  }
              };
      });
</script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body ng-controller="fCtrl">
  <p>Result:</p>
  <datan-type content="test1" con="{{xx}}"></datan-type>
</body>
</html>

Upvotes: 0

Views: 485

Answers (2)

rejo
rejo

Reputation: 3350

Try this method, its working http://plnkr.co/edit/NTG0LBa1dIPWcGGupJgt?p=preview

<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
<script>
        var app = angular.module('myApp', []);
      app.controller("fCtrl",function($scope){
        $scope.xx = ['x','c','y','z','a'];
        
      });
      
      app.directive('datanType', function ($compile) {
  return { 
    restrict: 'E',
    replace: true,
    link: function (scope, ele, attrs) {
        var testTemplate1 = '<h1 ng-repeat="x in arr">Test</h1>';
        var testTemplate2 = '<h1>Test2</h1>';
        var testTemplate3 = '<h1>Test3</h1>';
        var template = '';   
        scope.arr  = eval(attrs.con);
        switch(attrs.content){
            case 'test1':
                template = testTemplate1;
                break;
            case 'test2':
                template = testTemplate2;
                break;
            case 'test3':
                template = testTemplate3;
                break;
        }
        
        ele.html(template);
        $compile(ele.contents())(scope);  
      
    }
  };
});

      
     
</script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body ng-controller="fCtrl">
  <p>Result:</p>
  <datan-type content="test1" con="{{xx}}"></datan-type>
</body>
</html>

Upvotes: 1

doge1ord
doge1ord

Reputation: 311

This should work. Here are the following changes I did:

con is already binded to scope. no need to use attrs

var testTemplate1 = '<h1 ng-repeat="x in con">Test {{x}}</h1>';

Changed @ to = to bind scope property to parent scope (@ bounds as string, see this

   scope: {
      content: '=',
      con: '='
        }

Changed {{xx}} to xx

<datan-type content="test1" con="xx"></datan-type>

Working plunker http://plnkr.co/edit/P54mZhXWE7AnjfCWbQRb

Upvotes: 0

Related Questions