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 as follows:
<button ng-click="doIt()" spinner="spinIt">Spin It</button>
When the spinner value (i.e., the value of $scope.spinIt in this example) is true , the element should be hidden and spinnerButton should be displayed spinnerButton . If spinner is false , the element must be visible and spinnerButton must be hidden.
The problem here is that doIt() not in an isolated scope, so it doesn't click on a click.
What will be the Angular method for implementing this directive?
source share