/directives.js增加exampleDirective phonecatDirectives.directive('exampleDirective', function() { return { restrict: 'E', template: '<p>Hello {{number}}!</p>', controller: function($scope, $element){ $scope.number = $scope.number + "22222 &q…
 壹 ❀ 引 我在 angularjs 一篇文章看懂自定义指令directive 一文中简单提及了自定义指令中的link链接函数与compile编译函数,并说到两者具有互斥特性,即同时存在link与compile时link不生效.由于上篇博文篇幅问题,实在不好再过多讨论link,compile,那么本文将围绕三个问题展开,一是再识link与compile函数,你将知道两者为何互斥:二是了解link.compile与controller的区别,存在即合理,在合适的场景下应该使用哪个方法:三是了解指…
前言 在指令中存在controller和link属性,对这二者心生有点疑问,于是找了资料学习下. 话题 首先我们来看看代码再来分析分析. 第一次尝试 页面: <custom-directive></custom-directive> 脚本: angular .module('app',[]) .directive('customDirective', customDirective); function customDirective() { var directive = { r…
angjualrjs中的作用域与原生js中的函数嵌套原理一致,都是存在作用域的继承.若在子控制器(同样包括在指令中的link或是controllerding中定义变量,此时指令中必须未使用scope独立作用域)未定义相关变量,那么它会向父控制器一层层查找,直到找到位为止. 若在自定义指令中的link.controller与该指令的父控制器定义了同名变量,那它的作用域是如何的呢,以及指令中的独立作用域scope会对改变量产生怎样的影响,以例说明: HTML: <div ng-controller=…
其实严格来讲,link和controller是完全不同的概念,这里讲区别有点牵强. angular指令中,带有link和controller两个函数,很多人在写指令的时候不知道是写在link里 还是controller中. 其实这两个函数很大程度上牵扯到angular对于页面dom的加载时候对于两个函数的调用,可以理解为 link多涉及页面的dom结构 ,可以看做是$compile的一个回调 angularjs 对于drictive 必须是首先编译 ---> 调用$compile生成dom对象-…
The nitty-gritty of compile and link functions inside AngularJS directives  The nitty-gritty of compile and link functions inside AngularJS directives part 2: transclusion [译]ng指令中的compile与link函数解析 AngularJS directives are amazing. They allow you to…
The nitty-gritty of compile and link functions inside AngularJS directives  The nitty-gritty of compile and link functions inside AngularJS directives part 2: transclusion [译]ng指令中的compile与link函数解析 AngularJS directives are amazing. They allow you to…
1, controller 他会暴露一个API,利用这个API可以在多个指令之间通过依赖注入进行通信. controller($scope, $element, $attrs, $tranclude) 2, controllerAs 是给controller起个别名,方便使用 3, require 可以将其他指令传给自己 directiveName 通过驼峰法的命名指定了控制器应该带有哪一条指令,默认会从同一个元素的指令 ^directiveName 在父即查找指令 ?directiveName…
controller 会先跑,接着是view 里的ng-init,最后是link (指令里的). 所有在指令里如果用link去拿$attr,会有拿不到ng-init想setup的值…
今天研究指令嵌套时,发现子指令的link函数先于父指令的link函数执行. 这样和预想的顺序不一样. 也就是说,如果子指令的某个scope变量依赖于父指令传来的参数时,可能一直是undefinded比如: APP.directive("子指令", function () { return { scope: { 变量A:"=父指令的参数" }, restrict: 'A', replace: false, link: function (scope, elem, at…