I'm pretty new with Angular and I'm trying to write my own directive to learn how these objects works. My problems regard on how isolate the scope of my directive so I can use it many times in the same controller.
I created a plunker here to better explain the situation: http://plnkr.co/edit/NsISESR9sIs5Nf577DX4?p=preview
html:
<body ng-controller="MainCtrl">
<button id="button1" ng-click="dummyClickFoo()" wait-button>Foo</button>
<button id="button2" ng-click="dummyClickBar()" wait-button>Bar</button>
</body>
js:
app = angular.module('app', []);
app.controller('MainCtrl', function($scope) {
$scope.dummyClickFoo = function() {
$scope.startSpinner();
setTimeout(function() {
$scope.stopSpinner();
}, 3000);
};
$scope.dummyClickBar = function() {
$scope.startSpinner();
setTimeout(function() {
$scope.stopSpinner();
}, 3000);
};
});
app.directive('waitButton', function() {
return {
restrict: 'A',
controller: ['$scope', '$element', function($scope, $element) {
$scope.startSpinner = function() {
alert($element.attr('id'));
};
$scope.stopSpinner = function() {
alert($element.attr('id'));
};
}]
};
});
Basically it works with one button but it doesn't works with two buttons, I know I should isolate their scope but I don't know how... I've seen examples around, but they use extra-attributes to pass variables while I need to call my internal method.
Can someone help me to figure out how to do it? Thank you!