亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

AngularJS之指令中controller與link

標簽:
JavaScript

前言

在指令中存在controller和link属性,对这二者心生有点疑问,于是找了资料学习下。

话题

首先我们来看看代码再来分析分析。

第一次尝试

页面:

    >

脚本:

复制代码

angular    .module('app',[])    .directive('customDirective', customDirective); customDirective() {     {        restrict: 'EA',        template: '<div>{{vm.test}}</div>',        link: (){},        controller: directiveController,        controllerAs: 'vm'    };     directive;} directiveController() {    ;    vm.test = "I'm from Controller";}

复制代码

 

【注】:基础还是非常重要,页面上为custom-directive,在脚本我写成customdirective时死都没出效果,改成customDirective才好使。

第二次尝试

页面自定义指令不变,我们就修改下脚本:

复制代码

angular    .module('app',[])    .directive('customDirective', customDirective); customDirective() {     {        restrict: 'EA',        template: '<div>{{test}}</div>',        link: directiveLink    };     directive;} directiveLink(scope,elem,attr) {    scope.test = "I'm from Link";}

复制代码

到这里,我们不仅要开始思索:指令中的controller和link都可以实现同样的效果,那在指令中放这两个属性干嘛?我们的代码到底是放在controller还是link中?

我们首先来看看当二者一起使用时,呈现结果的顺序即在编译前后生成的顺序。

controller和link编译顺序

我们将脚本进行修改如下:

复制代码

angular    .module('app',[])    .directive('customDirective', customDirective); customDirective() {     {        restrict: 'EA',        template: '<div>xpy0928{{test}}</div>',        link: directiveLink,        controller:directiveController    };     directive;} directiveController($scope){    $scope.test = " from contrller cnblogs";} directiveLink(scope,elem,attr) {    scope.test = scope.test + ",and from link cnblogs";}

复制代码

生成如下:

我们由此得出结论:编译之前执行控制器(controller),编译之后执行链接(link)。

但是我们还未从根本上解决问题,在controller和link应该放哪些代码?我们接下来再看一个例子:

复制代码

,[]);    app.directive('customDirective', customDirective); customDirective() {     {        restrict: 'EA',        template: '<child-directive><child-directive>',      function($scope, $element) {            $element.find('span').text('hello cnblogs!');        }    };     directive;}app.directive("childDirective",childDirective); childDirective() {     {        restrict: 'EA',        template: '<h1>hello xpy0928</h1>',        replace: ,        link: ($scope, $element, attr) {                        $element.replaceWith(angular.element('<span>' + $element.text() + '</span>'));        }    }     directive;}

复制代码

此时结果应该还是hello xpy0928还是hello cnblogs呢?我们看下结果:

我们再来将如上进行修改看看:

复制代码

,[]);    app.directive('customDirective', customDirective); customDirective() {     {        restrict: 'EA',        template: '<child-directive><child-directive>',      function(scope, el) {            el.find('span').text('hello cnblogs!');        }    };     directive;}app.directive("childDirective",childDirective); childDirective() {     {        restrict: 'EA',        template: '<h1>hello xpy0928</h1>',        replace: ,        link: ($scope, $element, attr) {                        $element.replaceWith(angular.element('<span>' + $element.text() + '</span>'));        }    }     directive;}

复制代码

为什么会出现如此情况?因为在controller函数中此时所有child-directive指令中的link函数还未运行所以此时替换无效。

由此我们可以基本得出在controller和link中应该写什么代码的结论:

(1)在controller写业务逻辑(我们明白业务逻辑大部分是放在服务中),这里所说的业务逻辑乃是为呈现视图之前而准备的数据或者是与其他指令进行数据交互而暴露这个api。

(2)在link中主要操作DOM。

总结

指令乃是AngularJS中比较重要的一块,里面涉及到的东西也是非常之多,时不时的去往里面去灌东西,慢慢就会得心应手。

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消