2016-01-08 33 views
0

我正在嘗試使用角度種子項目(https://github.com/angular/angular-seed)啓動一個新項目。我正在嘗試添加新的指令。AngularJS角度種子啓動器項目添加指令

創建testDirective.js文件:

'use strict'; 

angular.module("myApp.testDirective", []) 
.directive("testDirective", 
     function() { 
      return { 
       template : "<h1>Made by a directive!</h1>" 
      }; 
     }); 

列入的index.html文件,添加 「myApp.testDirective」 到app.js,在view1.js我說:

'use strict'; 

angular.module('myApp.view1', ['ngRoute','myApp.testDirective']) 

.config(['$routeProvider', function($routeProvider) { 
    $routeProvider.when('/view1', { 
    templateUrl: 'view1/view1.html', 
    controller: 'View1Ctrl' 
    }); 
}]) 

.controller('View1Ctrl', [function() { 

}]); 

和view1.html看起來像:

<p>This is the partial for view 1.</p> 
<div testDirective></div> 

但是,當我運行該應用程序,什麼都沒有顯示testDirective的地方。不知道我出錯的地方。

回答

1

如果在HTML元素的開始標記內包含屬性指令,則必須將它們轉換爲破折號。

<div test-directive> 
+0

謝謝!我忘了所有關於... – erichardson30

2

只要你想在html上使用指令,你應該用大寫字母替換大寫字母-。指令標準化過程在編譯指令時將test-directive轉換爲testDirective

<div test-directive></div> 
+0

謝謝!我忘了所有關於... – erichardson30

+0

@ erichardson30很高興聽到它幫助..謝謝:) –