I am building an AngularJS directive. I want that directive to wrap other contents which have an ng-click inside of it. The resulting button does not do anything when clicked. Here is a simplified version of the code which I tried:
(HTML)
<div ng-app="someapp">
<div ng-controller="Ctrl1">
<h2>Example</h2>
<my-dir data-x="1">
<button ng-click="refresh()" id="refresh1">Refresh</button>
</my-dir>
<my-dir data-x="2">
<button ng-click="refresh()" id="refresh2">Refresh</button>
</my-dir>
</div>
</div>
(JavaScript)
var app = angular.module('someapp', []);
app.controller('Ctrl1', function($scope){ });
app.directive('myDir', function(){
return {
restrict: 'E',
scope: {},
template: '<div><p>Directive contents:</p><div ng-transclude></div></div>',
transclude: true,
link: function(scope, element, attrs){
$scope.y = attrs.x+1;
scope.refresh = function(){
console.log("Refresh Called, y = ", $scope.y);
}
}
};
});
How can I change it so that the button actually triggers the $scope.refresh() function?
Extra clarification:
I need local object information for the directive (there can be multiple of this directive in the single controller), so I create a new scope.