2014-04-16 42 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