2016-07-22 26 views
0

此代碼正常工作並使用父控制器作用域;在角度指令和調用對象函數上應用範圍

"<button type='button' post-random-feed >Post review</button>" 

.directive("postRandomFeed",['config',function(){ 
     return function(scope,element.attrs){ 

       element.bind("click", function(){ 
        scope.name ="henry"; 
        console.log(element); 
       }); 



    } 
}]); 

我的問題是,我想定義一個隔離的範圍,並返回一個對象而不是指令中的函數。這是我的代碼

"<button type='button' post-random-feed >Post review</button>" 

.directive("postRandomFeed",['config',function(){ 
     return { 

      restrict :"A", 

      scope : { 

        }, 

      irony : function(element, attrs) 
      { 
       element.bind("click", function(){ 
        console.log(element); 
       }); 


      } 
    }; 
}]); 

我如何調用該函數的諷刺使得元件註冊click事件任何錯誤請指正。謝謝

+0

你可以把在鏈接功能。 – ngLover

回答

0

這是你想要做的嗎? 您可以回調到控制器註冊:

HTML:

<button type='button' post-random-feed on-irony="onControllerFn">Post review</button> 

指令:

.directive("postRandomFeed",['config',function(){ 
     return { 
      restrict :"A", 
      scope : {}, 
      link : function(scope, element, attrs) { 
       $timeout(function() { 
        scope.$emit(attr.onIrony); 
       }); 
      }); 

     } 
    }; 
}]); 

,並在控制器,你可以這樣做:

$scope.$on('onControllerFn', function(event) { 
    // your code here 
}); 
+0

這是如何得到一個傳遞給控制器​​作用域的對象? – charlietfl

+0

在範圍內,我們可以指定它是否需要單向綁定或雙向綁定(如果需要的話)...我認爲問題是要獲取控制器的功能 – kukkuz

+0

謝謝。這正是我所需要的 – henrybbosa