指令模板必须只有一个根元素

Template for directive must have exactly one root element

我是 angularJs 的新手。我正在尝试创建包含输入元素和按钮的新指令。我想在单击按钮时使用此指令清除输入文本。

当我在 html 中使用我的指令时,出现以下错误:

Error: [$compile:tplrt] Template for directive 'cwClearableInput' must have exactly one root element. 

html:

<div class="input-group">
         <cw-clearable-input ng-model="attributeName"></cw-clearable-input>
 </div>

clearable_input.js:

angular.module('cw-ui').directive('cwClearableInput', function() {
  return {
    restrict: 'EAC',
    require: 'ngModel',
    transclude: true,
    replace: true,
    template: '<input type="text" class="form-control"/><span class="input-group-btn"><button type="button" class="btn" ng-click="" title="Edit"><span class="glyphicon-pencil"></span></button></span>',
    controller: function( $scope ) {

    }
};
});

我不知道如何实现。

只需用一些东西包裹您的模板:

template: '<div><input type="text" class="form-control"/><span class="input-group-btn"><button type="button" class="btn" ng-click="" title="Edit"><span class="glyphicon-pencil"></span></button></span></div>',

好吧,这个错误是不言自明的。您的模板需要有一个根,而您的有两个。解决此问题的最简单方法是将整个内容包装在 divspan:

template: '<div><input type="text" class="form-control"/><span class="input-group-btn"><button type="button" class="btn" ng-click="" title="Edit"><span class="glyphicon-pencil"></span></button></span></div>',

之前:

<input type="text" class="form-control"/>
<span class="input-group-btn">
  <button type="button" class="btn" ng-click="" title="Edit">
    <span class="glyphicon-pencil"></span>
  </button>
</span>

之后:

<div>    <!--  <- one root   -->
  <input type="text" class="form-control"/>
  <span class="input-group-btn">
    <button type="button" class="btn" ng-click="" title="Edit">
      <span class="glyphicon-pencil"></span>
    </button>
  </span>
</div>

如果模板路径不正确,也会发生此错误,在这种情况下,错误只是解释性的。

我指的是 templates/my-parent-template.html 中的模板(不正确)

template-url="subfolder/my-child-template.html"

我把这个改成

template-url="templates/subfolder/my-child-template.html"

解决了。

如果您不想创建一个根元素,您可以将 replace 设置为 false。在您的指令中,您将其设置为 true,这会将指令标记 "cw-clearable-input" 替换为指令的根元素。

当 none 对我有用时,在模板路径前加上 / 解决了我的问题。

function bsLoginModal() {
        return {
            restrict: 'A',
            templateUrl:'/app/directives/bootstrap-login-modal/template.html',
            link: linkFunction,
            controller: SignInCtrl,
            controllerAs: 'vm'
        }
    }

而不是

templateUrl:'app/directives/bootstrap-login-modal/template.html

祝你好运。

检查您的模板templateUrl

templateUrl: 'some/url/here.html'

template: '<div>HTML directly goes here</div>'

如果您在指令模板的根元素旁边的模板中有注释,您将看到相同的错误。

例如。如果你的指令模板有这样的 HTML(在指令 JS 中 "replace" 设置为 true),你将看到相同的错误

<!-- Some Comment -->
<div>
   <p>Hello</p>
</div>

因此,要在您想要保留评论的情况下解决此问题,您需要移动评论,使其位于模板根元素内。

<div>
   <!-- Some Comment -->
   <p>Hello</p>
</div>

我的问题是 webpack 的 HtmlWebpackPlugin 将脚本标签附加到指令内容。所以 angular 看到了:

<div>stuff</div>
<script type="text/javascript" src="directives/my-directive"></script>

解决方案是在 webpack.config.js:

中使用 HtmlWebpackPlugin 的 inject 选项
new HtmlWebpackPlugin({
  template: './src/my-directive.html',
  filename: './src/my-directive.html',
  inject: false
}),