Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs $compile()不会在自定义角度指令中激发_Angularjs_Angularjs Directive_Angularjs Scope - Fatal编程技术网

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>