我试图理解angular指令中链接函数的执行。但是,我陷入了一个非常简单的设置。以下是html代码:
<!doctype html>
<html ng-app="demoApp">
<body>
<div ng-controller="DemoController" class="container">
<directiveone directivetwo ></directiveone>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
<script src="app.js"></script>
</body>
</html>
&#34; demoApp&#34;在app.js中定义如下:
angular.module('demoApp', [])
.controller("DemoController", function(){
})
.directive("directiveone", function(){
var linker = function(scope, element, attrs){
console.log("directiveone link called");
};
return {
link : linker
}
})
.directive("directivetwo", function(){
var linker = function(scope, element, attrs){
console.log("directivetwo link called");
};
return {
require: "directiveone",
link : linker
};
});
当我运行此代码时,我收到错误:
Controller 'directiveone', required by directive 'directivetwo', can't be found!
我无法弄清楚这里的错误。文档表明这可能是一个错字,但对我来说并不明显。
答案 0 :(得分:1)
指令默认为仅属性。您的directiveone
指令未被提取 - 您需要在其定义中指定restrict: 'E'
,或将其应用为属性(<div directiveone directivetwo>...
)