2014-04-16 98 views
0
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> 

上面是我的指令和HTML指令的角码。我试图在使用编译的时候使用指令的“link:linkFn”。甚至有可能同时使用编译和链接指令?linkFn在角度指令中不起作用?

任何人都可以帮我解决这个问题吗?我错过了什么让它工作?

回答

0

当您使用编译功能时,它必须返回链接功能。否则它不会工作:

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

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

谢谢@Narretz它完美.. – Venkat

+0

嘿@Narretz这将是范围变量的链接功能范围有多大?我的意思是我正在改变范围变量标题,我在控制器中声明单击元素,但它没有显示任何变化..?编译:函数(元素,attrs){console.log('this is compile'); ('click',function(){ scope.title ='点击我的指令2';} }); }; } – Venkat

+0

如果您使用简单的jquery/javascript事件处理程序,您必须告诉角度在范围上发生更改:scope。$ apply(scope.title ='...') – Narretz