您需要引導模塊,在指令上設置restrict:'E',並將控制器聲明爲模塊。
var other = angular.module('other', []);
other.directive('something', function() {
return {
restrict: 'E',
template: '<div>Yep</div>',
link: function() {
console.log('something');
}
}
});
var myApp = angular.module('myApp', ['other']);
myApp.controller('MyCtrl',
function MyCtrl($scope) {
$scope.name = 'Superhero';
});
angular.element(document).ready(function() {
var moduleEl = document.getElementById("my-app");
angular.bootstrap(moduleEl, ['myApp']);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<section id="my-app">
<div ng-controller="MyCtrl">
Hello, {{name}}!
<something> Hrmmm </something>
</div>
</section>
在你的jsfiddle你只需要你的指令限制的元素角被期待它可以作爲一個屬性。例如http://jsfiddle.net/6Lwa8ohc/ – rob
請注意,您在jsfiddle中使用的Angular 1.0非常古老。在Angular 1.3+元素指令中默認是允許的 – rob