我怎么知道指令是完全用angularjs编译的 测试指令 angular.module(“myApp”,[]) .directive(“hello”,function()){ 返回{ 范围:{ 警报:“=” }, 链接:功能(范围、要素、属性){ scope.alert=函数(msg){ window.alert(msg) } } } }) .controller(“bodyController”,函数($scope,$timeout){ $scope.outAlert(“发生错误:未定义不是函数”) $timeout(函数(){ $scope.outAlert(“这很好用!”) }, 100) })

我怎么知道指令是完全用angularjs编译的 测试指令 angular.module(“myApp”,[]) .directive(“hello”,function()){ 返回{ 范围:{ 警报:“=” }, 链接:功能(范围、要素、属性){ scope.alert=函数(msg){ window.alert(msg) } } } }) .controller(“bodyController”,函数($scope,$timeout){ $scope.outAlert(“发生错误:未定义不是函数”) $timeout(函数(){ $scope.outAlert(“这很好用!”) }, 100) }),angularjs,directive,compiled,Angularjs,Directive,Compiled,如上代码所示,hello指令将outAlert功能传输到bodyController。但是bodyController不能立即使用outAlert,否则会出现错误“未定义不是功能” 所以我必须在100毫秒后运行它。但这看起来不像一个正式的解决方案。我想寻找更好的方法 我怎么知道指令已经在angularjs中完全编译了???在我这几个月的实践之后,最好在指令中向外部组件提供回调,因为如果它调用自己提供的回调,指令必须完全编译 (我的英语很差!你能理解吗?经过我这几个月的练习,最好在指令中向外部组件

如上代码所示,
hello
指令将
outAlert
功能传输到
bodyController
。但是
bodyController
不能立即使用
outAlert
,否则会出现错误“未定义不是功能”

所以我必须在100毫秒后运行它。但这看起来不像一个正式的解决方案。我想寻找更好的方法


我怎么知道指令已经在angularjs中完全编译了???

在我这几个月的实践之后,最好在指令中向外部组件提供回调,因为如果它调用自己提供的回调,指令必须完全编译


(我的英语很差!你能理解吗?

经过我这几个月的练习,最好在指令中向外部组件提供回调,因为如果它调用自己提供的回调,指令必须完全编译


(我的英语很差!你能理解吗?

你正在重新定义在
链接中传递到指令中的警报。没有道理。创建一个包含基本html的演示,并复制您的问题。同样没有意义的是,你的控制器不能使用其中定义的函数,除非你正在覆盖它,如果它也被传递到链接中,你想用它实现什么?我认为如果试图与函数对象进行双向绑定,您应该使用带有
$on
$emit
的自定义范围事件。您正在重新定义传递到
链接中的指令中的警报。没有道理。创建一个包含基本html的演示,并复制您的问题。同样没有意义的是,你的控制器不能使用其中定义的函数,除非你正在覆盖它,如果它也被传递到链接中,你想用它实现什么?我认为如果试图与函数对象进行双向绑定,则应该使用带有
$on
$emit
的自定义范围事件。
<!DOCTYPE html>
<html ng-app="myApp">
<head>
  <title>test directive</title>
</head>
<body ng-controller="bodyController">

  <hello alert="outAlert"></hello>

  <script type="text/javascript" src="http://cdn.bootcss.com/angular.js/1.4.8/angular.js"></script>
  <script type="text/javascript">
  angular.module("myApp", [])

  .directive("hello", function(){
    return {
      scope: {
        alert: "="
      },
      link: function(scope, elem, attrs){
        scope.alert = function(msg){
          window.alert(msg)
        }
      }
    }
  })

  .controller("bodyController", function($scope, $timeout){
    $scope.outAlert("this occurs an error: undefined is not a function")

    $timeout(function(){
      $scope.outAlert("this works great!")
    }, 100)
  })
  </script>
</body>
</html>