无法从指令中的模板编译ng-repeat

时间:2016-07-29 10:49:14

标签: javascript html angularjs dynamic compilation

我正在尝试使用JS动态添加表单元素,并且需要一个指令。我能够添加表单元素但是当我有ng-options或ng-repeat时它不会被编译。我有一个我用于演示的示例指令。

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>

2 个答案:

答案 0 :(得分:1)

尝试此方法,其工作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>

答案 1 :(得分:0)

这应该有效。以下是我所做的以下更改:

con已绑定到范围。不需要使用attrs

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

将@ to =更改为将范围属性绑定到父范围(@ bounds as string,请参阅this

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

将{{xx}}更改为xx

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

工作人员http://plnkr.co/edit/P54mZhXWE7AnjfCWbQRb