使用 link 属性与使用带有 pre-link 和 post-link 的编译有什么区别?

What is the difference between using the link attribute vs using compile with pre-link and post-link?

This question 是关于在指令定义中使用编译属性的 pre-link 和 post-link 的非常有用的信息。但是,它没有说明直接使用 link 属性意味着什么。我说的是这两者的区别:

甲:

myApp.directive('log', function() {
    return {
      link: function(...){...},
      // other stuff here
    };
});

乙。

myApp.directive('log', function() {  
    return {  
      compile: function(...){
          return {
            pre: function preLink( scope, element, attributes ) {
               // stuff
            },
            post: function postLink( scope, element, attributes ) {
                // stuff
            }
          }
      },  
      // other stuff here  
    };  
});  

什么是 link 函数?它应该结合 pre 和 post 吗?我不明白如何以不同方式使用这两种模式。

没有功能差异 - 只是 API "sugar":

link: function postlink(){...}

是API的快捷方式:

link: {
  post: function postlink(){...}
}

完整形式是:

link: {
  pre: function prelink(){...},
  post: function postlink(){...}
}

它本身就是

的快捷方式
compile: function(){
  return {
     pre: function prelink(){...},
     post: function postlink(){...}
  };
}

其中,可以稍微简化为:

compile: function(){
  return function postlink(){...}
}

当指定compile属性时,link属性被忽略。

这是 Angular 关于这个主题的 documentation