Javascript AngularJS自定义指令don';我不能生成代码

Javascript AngularJS自定义指令don';我不能生成代码,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我是AngularJS的新手,看到我所有的html代码变得越来越庞大和肮脏,我想做一些指令来组织它。所以,我从一个小指令开始,你们可以在这里看到: app.module('MyDirectiveModule', [ ]) .directive('TestDirective', function () { return { restrict: 'E', templateUrl: 'test-index.html' }; }); 对于这段代码: <div cla

我是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>

使用AngularJs编码

因此最终结果是(html指令文件是test index.html):



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

您正在使用
调用您的指令,但您的指令的名称是
TestDirective
。您应该使用


另外,如果您使用的是Angular 1.5,我建议您查看组件而不是指令。这将使到Angular2的转换更加平滑。

您使用
调用指令,但指令的名称是
TestDirective
。您应该使用

另外,如果您使用的是Angular 1.5,我建议您查看组件而不是指令。这将使到Angular2的过渡更加平滑。

您有:

<body ng-app="testsApp">
如果您没有
testsApp
模块,则将ng app值替换为指令模块:

<body ng-app="MyDirectiveModule">

正如@Baruch所提到的,您的指令的元素应该更改为:

<test-directive></test-directive>

您有:

<body ng-app="testsApp">
如果您没有
testsApp
模块,则将ng app值替换为指令模块:

<body ng-app="MyDirectiveModule">

正如@Baruch所提到的,您的指令的元素应该更改为:

<test-directive></test-directive>


将指令名称更改为testIndex,而不是TestDirective。它将把指令名改为testIndex而不是TestDirective。它会工作的,这就是问题所在,谢谢(指令名,它必须有一个小写的第一个字符),现在我有一个模板加载错误,但我认为这是一个parth问题,这就是问题所在,谢谢(指令名,它必须有一个小写的第一个字符),现在我有一个模板加载错误,但我认为这是一个parth问题