2017-02-08 2 views
0

Я имею в виду документацию AngularJS, но не смог получить «Директиву по комментариям», работающий так, как упоминалось на самом деле, столкнулся с ошибкой. Здесь Вы можете найти подробную информацию:Угловая директива как комментарий не работает

HTML: -

<!DOCTYPE html> 
<html> 
<head> 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script> 
    <script type="text/javascript" src="JS/DirectiveAsComment.js"></script> 
    <title>Directive as Comment</title> 
</head> 
<body ng-app="directiveAsCommentApp"> 
    <!-- directive:test-comment-directive --> 
</body> 
</html> 

JS: -

var app = angular.module('directiveAsCommentApp', []); 

app.directive('testCommentDirective', function(){ 
return { 
     restrict: 'M', 
     replace: true, 
     template: 'this text is displayed because of "test-comment-directive" custom directive' 
}; 
}); 

Ошибка: [$ компиляции: tplrt] Шаблон для директивы 'testCommentDirective' должен иметь ровно один корневой элемент.

пожалуйста, помогите мне решить эту проблему ..

ответ

2

When a directive is declared with template (or templateUrl) and replace mode on, the template must have exactly one root element. That is, the text of the template property or the content referenced by the templateUrl must be contained within a single html element. For example, <p>blah <em>blah</em> blah</p> instead of simply blah <em>blah</em> blah. Otherwise, the replacement operation would result in a single element (the directive) being replaced with multiple elements or nodes, which is unsupported and not commonly needed in practice.

Изменить этот

template: 'this text is displayed because of "test-comment-directive" custom directive'

в

template: '<div>this text is displayed because of "test-comment-directive" custom directive</div>' 

Пожалуйста, исх: https://docs.angularjs.org/error/ $ компилировать/tplrt

0

Проблема с вашим шаблоном, записать их в несколько тегов, как и или: попробовать этот

<!DOCTYPE html> 
<html> 
    <head> 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">  </script> 
     <title>Directive as Comment</title> 
    </head> 
    <body ng-app="myApp"> 
     <!-- directive:test-comment-directive --> 
    </body> 
</html> 

и app.js попробовать этот

var app = angular.module('myApp', []); 

app.directive('testCommentDirective', function(){ 
    return { 
     restrict: "M", 
     replace: true, 
     template: "<div>this text is displayed because of 'test-comment-directive' custom directive</div>" 
      }; 
    }); 
Смежные вопросы