在AngularJS中,自定义Directive过程中,有时用link和controller都能实现相同的功能。那么,两者有什么区别呢?

使用link函数的Directive

页面大致是:

<button id="addItem">Add Item</button>
<without-Controller datasource="customers" add="addCustomer"></without-Controller>

Directive方面:

(function(){
var withoutController = function(){
var tempalte = '<button id="addItem">Add Item</button><div></div>'; var link = function(scope, element, attrs){ //从scope中的datasource拿到数据源
var items = angular.copy(scope.datasource),
button = angular.element(document.getElementById('addItem')); button.on('click', addItem); render(); function addItem(){
var name = 'new customer'; //执行Directive中传入的方法,带参数
scope.$apply(function(){
scope.add()(name);
}); items.push({
name: name
}); render();
} function render(){
var html = '<ul>';
for(var i=0, len=item.length;i<len;i++){
html += '<li>' + items[i].name + '</li>'
}
html += '</ul>'; element.find('div').html(html);
}
}; reutrn {
restrict: 'EA',
scope: {
datasource: '=',
add: '&'
},
link: link,
template: template
}
}; angular.module('directiveModule')
.directive('withoutController', withoutController);
}());

使用controller的Directive

页面大致是:

<with-controller datasource="customers" add="addCustomer"></with-controller>

Directive方面:

(function(){
var withController = function(){
var template = '<button ng-click="addItem()">Add Item</button><ul>' + '<li ng-repeat="item in items">{{::item.name}}</li></ul>', controller = ['$scope', function($scope){
init(); function init(){
$scope.items = angular.copy($scope.datasource);
} $scope.addItem = function(){
var name = "customer new";
$scope.add()(name);
$scope.items.push({
name: name
});
}
}]; return {
restrict: 'EA',
scope: {
datasource: '=',
add:'&'
},
controller: controller,
template:template
}
}; angular.module('directiveModule')
.direcitve('withController', withController);
}());

可见,link和controller的相同点在于里面都可包含数据源和操作。不同点在于:link能控制渲染html元素的过程,而controller不能,controller的模版写死的,仅侧重于提供数据源和操作。

如果使用controllerAs,Directive大致是:

(function(){
var withController = function(){
var template = '<button ng-click="vm.addItem()">Add Item</button><ul>' + '<li ng-repeat="item in vm.items">{{::item.name}}</li></ul>', controller = function(){
var vm = this; init(); function init(){
vm.items = angular.copy($scope.datasource);
} vm.addItem = function(){
var name = "customer new";
vm.add()(name);
vm.items.push({
name: name
});
}
} return {
restrict: 'EA',
scope: {
datasource: '=',
add:'&'
},
controller: controller,
controllerAs: 'vm',
bindToController:true,
template:template
}
}; angular.module('directiveModule')
.direcitve('withController', withController);
}());

其中,controllerAs和bindToController属性成对出现。

AngularJS自定义Directive中link和controller的区别的更多相关文章

  1. AngularJs 指令 directive中link,controller 的区别

    其实严格来讲,link和controller是完全不同的概念,这里讲区别有点牵强. angular指令中,带有link和controller两个函数,很多人在写指令的时候不知道是写在link里 还是c ...

  2. AngularJs中,如何在父元素中调用子元素为自定义Directive中定义的函数?

    最近一段时间准备使用AngularJs中的自定义Directive重构一下代码. 在这里说明一下,把自定义控件封装成Directive并不一定是要复用,而是要让代码结构更加清晰.就好像你将一个长方法拆 ...

  3. AngularJS自定义Directive与controller的交互

    有时候,自定义的Directive中需要调用controller中的方法,即Directive与controller有一定的耦合度. 比如有如下的一个controller: app.controlle ...

  4. AngularJS自定义Directive不一定返回对象

    AngularJS中,当需要自定义Directive时,通常返回一个对象,就像如下的写法: angular.module('modulename') .directive('myDirective', ...

  5. AngularJS自定义Directive初体验

    通常我们这样定义个module并随之定义一个controller. var app = angular.module('myApp', []); app.controller('CustomersCo ...

  6. AngularJS自定义Directive

    (编辑完这篇之后,发现本篇内容应该属于AngularJS的进阶,内容有点多,有几个例子偷懒直接用了官方的Demo稍加了一些注释,敬请见谅). 前面一篇介绍了各种常用的AngularJS内建的Direc ...

  7. [AngularJS] Reusable directive, require from parent controller

    Glorious Directives for Our Navigation NoteWrangler navigation has now been broken into two parts: t ...

  8. CSS中link与import的区别

    一.import的用法 1,在html文件中 <style type="text/css"> @import url(http://www.dreamdu.com/st ...

  9. SpringBoot 中 @RestController 和 @Controller 的区别

    1 - 在springboot中,@RestController 相当于 @Controller + @ResponseBody;2 - 即在Controller类中,若想返回jsp或html页面,则 ...

随机推荐

  1. skearn自学路径

    sklearn学习总结(超全面) 关于sklearn,监督学习几种模型的对比 sklearn之样本生成make_classification,make_circles和make_moons pytho ...

  2. php中相对路径和绝对路径如何使用(详解)

    目录 一.总结 一句话总结: 1.php中用用“/”表示根目录么? 2.什么符号表示当前目录(asp,jsp,php都一样)? 3.php中如何使用$_SERVER['DOCUMENT_ROOT']做 ...

  3. main.js 里的/* eslint-disable no-new */

    注意项目中的这个,它的作用是: 在js里面,new 一个对象,需要赋值给某个值(变量),用Vue实例化的时候,不需要赋值给值(变量),所以要单独给配一条规则,给new Vue这行代码上面加这个注释,把 ...

  4. STM32F412应用开发笔记之七:片上ADC的应用测试

    在我们的应用项目中需要采集一些模拟量,这些量使用MCU自带的ADC就可以满足要求.在NUCLEO-F412ZG实验板上的STM32F412ZG有一个16通道的ADC,我们试验用它采集几个数据. 在NU ...

  5. [转]centos7 下安装MongoDB

    查看MongoDB的最新版官方下载地址: https://www.mongodb.com/download-center#community 使用wget命令下载安装包 wget https://fa ...

  6. python易错题之lambda 以及 for循环中内嵌函数

    li = [] for x in range(10): print(x) //在函数没有执行前(li[0]()),for 循环中x已经执行完,x会一直为 9 def fun(): print(x) / ...

  7. vtiger自定上传图片的字段

    废话不多说,上一篇有说到过vtiger新建模块的事 现在我新建了一个Score的模块,里面需要一个上传图片的功能 在Score界面新建一个字段叫grede 但是自定义类型里面是没有,图片这个选项的,只 ...

  8. JDK1.7+Tomcat6.0+MyEclipse8.6在win7下的安装与配置

    http://wenku.baidu.com/view/4f0bef02192e45361066f548.html

  9. 2015 Benelux Algorithm Programming Contest I- Interesting Integers

    题目大意:给你一个数字n(n<=1e9) ,让你求一个能包含这个数的斐波那契数列的第一项a 和第二项b,找出b最小的那个. 帮我复习了一下扩展欧几里得.... 思路:a,b,a+b,a+2b…… ...

  10. 6-10 下落的树叶 uva699

    类似第九题  都是属于比较巧妙的题目  ! 用一个p数组来保存水平值   然后开始built 自然就会按照自左而右的顺序来读取!!!!!!这很重要 #include<bits/stdc++.h& ...