javascript - AngularJS 自定义指令不生成代码

标签 javascript angularjs angularjs-directive

我是 AngularJS 的新手,看到我所有的 html 代码变得越来越庞大和肮脏,我想做指令来组织它。因此,我从一个小指令开始,您可以在这里看到:

app.module('MyDirectiveModule', [ ])

.directive('TestDirective', function () {
  return {
    restrict: 'E',
    templateUrl: 'test-index.html'
  };
});

对于这一小段代码:

  <div class="text-center">
      <h1>Coding with AngularJs</h1><br><br>
  </div>

所以最终结果是(html指令文件为test-index.html):

<body ng-app="testsApp">

  <test-index></test-index>

我包含了所有脚本并且检查了所有路径,我仍然不明白为什么它仍然不起作用..

最佳答案

您正在使用 <test-index></test-index> 调用您的指令但你的指令名称是 TestDirective 。您应该使用 <test-directive></test-directive> 来调用它

此外,如果您使用 Angular 1.5,我建议您研究组件而不是指令。这将使向 Angular2 的过渡更加顺利。

关于javascript - AngularJS 自定义指令不生成代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38333948/

相关文章:

javascript - jQuery 函数无法通过 id 获取元素

angularjs - 如何让这个过滤器变得更好

angularjs - 不允许在文本字段中使用空格的 Angular 限制

javascript - 如何使用 angularjs 和 ionic 更新这个 JSON 数组值

javascript - Angular Directive(指令)不更新模型

AngularJS - 表单验证的指令范围问题

javascript - extjs中的分组(数据在分组行的列中)

javascript - 在引导模式窗口之外选择下拉选择会导致模式关闭

javascript - 在访问者访问的每个网页上叠加一个框架?

angularjs - 当数组中的对象列表时,ng-repeat 中不允许出现重复错误