Angularjs $compile()不会在自定义角度指令中激发
我有以下指示:Angularjs $compile()不会在自定义角度指令中激发,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,我有以下指示: angular.module("example_module", []) .directive("example_directive", function() { return { compile: function(element) { element.html('{{example}}'); return function($scope) { $scope.example = "Hello world"; };
angular.module("example_module", [])
.directive("example_directive", function() {
return {
compile: function(element) {
element.html('{{example}}');
return function($scope) {
$scope.example = "Hello world";
};
}
};
});
以及以下HTML代码:
<!DOCTYPE html>
<html ng-app="example_module">
<head>
<meta charset="utf-8">
<title>Example title</title>
<script src="lib/angular/angular.min.js"></script>
<script src="js/example.js"></script>
</head>
<body>
<div example_directive></div>
</body>
</html>
示例标题
我希望指令编译为helloworld
,但它编译为空字符串。错误在哪里
我可以使用
模板
或链接
函数,但我的目标是了解编译
函数的工作原理。这与如何处理指令名有关。我已经更改了您的示例,以匹配angular的命名约定,并将
angular.module(“示例_模块”,[])
.directive(“exampleDirective”,函数(){
返回{
编译:函数(元素){
html('{example}}');
返回函数($scope){
$scope.example=“Hello world”;
};
}
};
});
angular.module("example_module", [])
.directive("exampleDirective", function() {
return {
compile: function(element) {
element.html('{{example}}');
return function($scope) {
$scope.example = "Hello world";
};
}
};
});
<body>
<div example-directive></div>
</body>