AngularJS指令未显示

AngularJS Directive Not Showing

本文关键字:显示 指令 AngularJS      更新时间:2023-09-26

这已经把我逼上了好几个小时。

我有我的app.js

    'use strict';
/**
 * @ngdoc overview
 * @name myWellnessTrackerApp
 * @description
 * # myWellnessTrackerApp
 *
 * Main module of the application.
 */
angular
  .module('myWellnessTrackerApp', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch'
  ])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .when('/about', {
        templateUrl: 'views/about.html',
        controller: 'AboutCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  });

我的main.html,它在加载时加载到主页中,如上所示。

<innerPageHeader> </innerPageHeader>

我的指示。

angular.module('myWellnessTrackerApp')
  .directive('innerPageHeader' ,function () {
    return {
      template: '<h4>TEST</h4>',
      restrict: 'E'
    };
  });

我的index.html草稿:

<!-- build:js(**/*) scripts/vendor.js -->
<!-- bower:js -->
<script src="components/modernizr/modernizr.js"></script>
<script src="components/jquery/dist/jquery.js"></script>
<script src="components/angular/angular.js"></script>
<script src="components/angular-animate/angular-animate.js"></script>
<script src="components/angular-cookies/angular-cookies.js"></script>
<script src="components/angular-resource/angular-resource.js"></script>
<script src="components/angular-route/angular-route.js"></script>
<script src="components/angular-sanitize/angular-sanitize.js"></script>
<script src="components/angular-touch/angular-touch.js"></script>
<script src="components/fastclick/lib/fastclick.js"></script>
<script src="components/jquery.cookie/jquery.cookie.js"></script>
<script src="components/jquery-placeholder/jquery.placeholder.js"></script>
<script src="components/foundation/js/foundation.js"></script>
<!-- endbower -->
<!-- endbuild -->
<!-- build:js({.tmp,app}) scripts/scripts.js -->
<script src="scripts/app.js"></script>
<script src="scripts/controllers/main.js"></script>
<script src="scripts/controllers/about.js"></script>
<script src="scripts/directives/innerpageheader.js"></script>

加载页面时,它只显示

<innerPageHeader> </innerPageHeader>

没有应该是的指令内容

<innerPageHeader><h4>TEST</h4> </innerPageHeader>

您需要使用如下指令。

<inner-page-header></inner-page-header>

我相信您已经了解了这一点,但原因是由于规范化。在规范化过程中,Angular将分隔的名称转换为camelCase。

这就是为什么在AngularJS中指定指令时应该使用camelCase。