Angularjs the difference between link and controller in custom directive

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.