In Angularjs, the same functionality is sometimes achieved with both link and controller in a custom directive process. So what's the difference between the two?
Directive using the link function
The page is roughly:
<button id= "AddItem" >add item</button>
<without-controller datasource= "Customers" add= "Addcustomer" ></without-Controller>
Directive aspects:
(function(){ varWithoutcontroller =function(){ varTempalte = ' <button id= ' addItem ' >add item</button><div></div> '; varlink =function(scope, element, Attrs) {//get the data source from the DataSource in scope varItems =angular.copy (scope.datasource), Button= Angular.element (document.getElementById (' AddItem '))); Button.on (' Click ', AddItem); Render (); functionAddItem () {varName = ' New Customer '; //executes the method passed in the directive, with parametersScope. $apply (function() {scope.add () (name); }); Items.push ({name:name}); Render (); } functionrender () {varhtml = ' <ul> '; for(varI=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);} ());
Using the controller's directive
The page is roughly:
<with-controller datasource= "Customers" add= "Addcustomer" ></with-controller>
Directive aspects:
(function(){ varWithcontroller =function(){ varTemplate = ' <button ng-click= ' AddItem () ">add item</button><ul> ' + ' <li ng-repeat=" Item in Items " >{{::item.name}}</li></ul> ', Controller= [' $scope ',function($scope) {init (); functioninit () {$scope. Items=angular.copy ($scope. DataSource); } $scope. AddItem=function(){ varname = "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);} ());
As can be seen, link and controller have the same point in which both the data source and operation are included. The difference is that link can control the process of rendering HTML elements, and controller cannot, the controller's template is written dead, focusing only on providing data sources and operations.
If using controlleras,directive is roughly:
(function(){ varWithcontroller =function(){ varTemplate = ' <button ng-click= ' Vm.additem () ' >add item</button><ul> ' + ' <li ng-repeat= ' Item in Vm.items ">{{::item.name}}</li></ul>", Controller=function(){ varVM = This; Init (); functioninit () {Vm.items=angular.copy ($scope. DataSource); } Vm.additem=function(){ varname = "Customer New"; Vm.add () (name); Vm.items.push ({name:name}); } } return{restrict:' EA ', scope: {datasource:=, add:' & '}, Controller:controller, Controlleras:' VMS ', Bindtocontroller:true, template:template}}; Angular.module (' Directivemodule '). DIRECITVE (' Withcontroller ', Withcontroller);} ());
Where the Controlleras and Bindtocontroller properties appear in pairs.
Angularjs the difference between link and controller in custom directive