2016-07-22 76 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