简体   繁体   中英

linkFn in angular directives is not working?

var $directive = angular.module('myApp', []);
    $directive.directive('chandanSingh', function(){
      return {
          restrict: 'E',
         compile:function(element, attrs){
               console.log('This is complie');
         },
         link: function(scope, element, attrs){
              console.log('This is link');
         },
         template: '<h4>{{title}}</h4>',
         controller:function($scope){
               $scope.title = "My Directive";
         }
    };
});

<chandan-singh></chandan-singh>

Above is my angular code for directive and HTML directive. i am trying to use "link: linkFn" of directives while using compile in it. Is it even possible to use both compile and link for same directive?

Can anyone help me out with this? what is it i am missing to make it work?

when you use a compile function, it must return the link function. Otherwise it won't work:

     compile:function(element, attrs){
           console.log('This is complie');

           return function link(scope, element, attrs){
                console.log('This is link');
           },
     },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM