2个“postLink"有什么区别?指令中的功能? [英] What's the difference between the 2 "postLink" functions in directive?

查看:17
本文介绍了2个“postLink"有什么区别?指令中的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从angularjs的文档来看,在定义指令时,compile中有一个postLinklink<中有一个postLink/代码>

From the doc of angularjs, when defining a directive, there's a postLink in compile, and a postLink in link

myModule.directive('directiveName', function factory(injectables) {
  var directiveDefinitionObject = {
    priority: 0,
    template: '<div></div>',
    templateUrl: 'directive.html',
    replace: false,
    transclude: false,
    restrict: 'A',
    scope: false,
    compile: function compile(tElement, tAttrs, transclude) {
      return {
        pre: function preLink(scope, iElement, iAttrs, controller) { ... },
        post: function postLink(scope, iElement, iAttrs, controller) { ... }
      }
    },
    link: function postLink(scope, iElement, iAttrs) { ... }
  };
  return directiveDefinitionObject;
});

它们之间有什么区别?我注意到 link 中的 postLink 的参数小于 compile 中的参数.还有其他区别吗?

What's the difference between them? I notice the postLink in link has a argument less than the one in compile. And are there any other difference?

推荐答案

它们没有什么不同,您所拥有的只是文档中的伪代码.postLink函数只是最重要的一个,所以有多种声明方式.

They're no different, what you have there is just psuedo-code from the documentation. The postLink function is just the most important one, so there are a variety of ways to declare it.

这里以 Plunker 为例...

... 下面是一些显示 postLink 函数不同声明的伪代码:

... and here is some psuedo code showing the different declarations of a postLink function:

app.directive('dir1', function () {
   return function(scope, elem, attr) {
       //this is the same
   };
});

app.directive('dir2', function () {
   return {
       link: function(scope, elem, attr) {
           //this is the same
       }
   };
});

app.directive('dir3', function () {
   return {
      compile: function compile(tElement, tAttrs, transclude) {
         return {
           post: function postLink(scope, elem, attrs) {
              //this is the same
           }
         }
      }
   };
});

...你只需要一个.

这篇关于2个“postLink"有什么区别?指令中的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆