Consider the following directive: (Live Demo)
app.directive('spinner', function() {
return {
restrict: 'A',
scope: {
spinner: '=',
doIt: "&doIt"
},
link: function(scope, element, attrs) {
var spinnerButton = angular.element("<button class='btn disabled'><i class='icon-refresh icon-spin'></i> Doing...</button>");
element.after(spinnerButton);
scope.$watch('spinner', function(showSpinner) {
spinnerButton.toggle(showSpinner);
element.toggle(!showSpinner);
});
}
};
});
which is used like this:
<button ng-click="doIt()" spinner="spinIt">Spin It</button>
When spinner
's value (i.e. the value of $scope.spinIt
in this example) is true
, the element should be hidden and spinnerButton
should appear instead. When spinner
's value is false
, the element should be visible and spinnerButton
should be hidden.
The problem here is that doIt()
is not in the isolated scope, thus not called on click.
What would be the "Angular way" to implement this directive?