【问题标题】:Unable to compile ng-repeat from template in directive无法从指令中的模板编译 ng-repeat
【发布时间】:2016-12-04 01:27:07
【问题描述】:

我正在尝试使用 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>

【问题讨论】:

  • 而不是使用真正的switch 使用ng-switch
  • 你能在 plunker 里给我看一下吗?它是在控制器还是指令中。我基本上希望 ng-repeat 工作

标签: javascript html angularjs dynamic compilation


【解决方案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>

【讨论】:

【解决方案2】:

这应该可行。以下是我所做的更改:

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

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

将 @ 更改为 = 以将作用域属性绑定到父作用域(@ 界限为字符串,请参阅 this

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

将 {{xx}} 更改为 xx

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

打工仔http://plnkr.co/edit/P54mZhXWE7AnjfCWbQRb

【讨论】:

猜你喜欢
  • 2017-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多