2013-08-19 30 views
1

按鈕內的範圍,我需要有一個可重複使用的指令與幾個buttons.But我的問題是,當我嘗試在同一指令的多次按鍵結合使用一個單獨的範圍。 例如看到下面的代碼:如何分離可重複使用的指令

<body ng-controller="MainCtrl"> 
    <test></test> 
    <test></test> 
</body> 

代碼測試指令是

app.directive("test",function(){ 

return{ 
    restrict:"E", 
    scope:{ 
      }, 
    template:"<input type='button' value='click me!' onclick='clickD()'>", 
    controller:function($scope){ 
     clickD=function() 
     { 
      alert($scope.$id); 
     } 
    } 
} 
}) 

你可以在這裏看到的例子以及link

現在怎麼辦分別在我範圍。 我的實際問題很笨拙,所以我簡化它直到這個level.Please幫我!

回答

2

你需要使用ng-click而不是onclick,還放置clickD()功能上的$scope

app.directive("test", function() { 

    return { 
     restrict: "E", 
     scope: {}, 
     template: "<input type='button' value='click me!' ng-click='clickD()'>", 
     controller: function ($scope) { 
      $scope.clickD = function() { 
       alert($scope.$id); 
      } 
     } 
    } 
}) 

updated plnkr

+0

感謝Mark.It的工作現在:) – Crypt