Skip to content

Instantly share code, notes, and snippets.

@eperedo
Created June 5, 2013 20:50
Show Gist options
  • Save eperedo/5717222 to your computer and use it in GitHub Desktop.
Save eperedo/5717222 to your computer and use it in GitHub Desktop.
AngularJs Directive for ladda buttons - https://github.com/hakimel/Ladda
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, $timeout) {
$scope.save = function(){
$scope.loading = true;
$timeout(function(){
$scope.loading = false;
}, 3000);
};
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="http://lab.hakim.se/ladda/css/ladda.css" />
<script data-require="[email protected]" src="http://code.angularjs.org/1.1.5/angular.js" data-semver="1.1.5"></script>
<script src="http://lab.hakim.se/ladda/js/ladda.js"></script>
<script src="app.js"></script>
<script src="directive.js"></script>
</head>
<body ng-controller="MainCtrl">
<form data-ng-submit="save()">
<button id="w" type="submit" class="ladda-button blue zoom-in" ladda data-ng-model="loading">
<span class="label">Submit</span>
<span class="spinner"></span>
</button>
</form>
</body>
</html>
app.directive('ladda', function(){
return {
require: '?ngModel',
restrict: 'A',
link : function postLink(scope, attr, elem, ctrl){
var l = Ladda.create( document.querySelector('#'+elem.id));
scope.$watch('loading', function(newVal, oldVal){
if (newVal !== undefined){
if(newVal)
l.start();
else
l.stop();
}
});
}
};
});
@Maidomax
Copy link

Maidomax commented Feb 7, 2014

I've had to change some of the code to make it work with the new versions of Ladda and AngularJS

The buttons look like this now:

    <button id="w" type="submit" class="ladda-button" data-color="blue" data-style="zoom-in" ladda="loading">
      <span class="ladda-label">Submit</span>
    </button>

(The text needs to be wrapped inside a span.ladda-label, the value of the ladda attribute is the value on the scope that is being watched.)

The directive looks like this:

    app.directive('ladda', function(){
        return {
            restrict: 'A',
            link : function(scope, element, attrs){
                var l = Ladda.create( document.querySelector('#'+attrs.id));
                scope.$watch(attrs.ladda, function(newVal, oldVal){
                    if (newVal !== undefined){
                        if(newVal) {
                            l.start();
                        }
                        else {
                            l.stop();
                        }
                    }
                });
            }
        };
    });

@ValentinH
Copy link

I modified the code in order to use it within a ngRepeat loop. With this version, there is no need to specify an id for each button. The element is enough.

app.directive('ladda', function() {
    return {
      restrict: 'A',
      link: function(scope, element, attrs) {
        if (element && element[0]) {
          var l = Ladda.create(element[0]);
          scope.$watch(attrs.ladda, function(newVal, oldVal) {
            if (newVal !== undefined) {
              if (newVal)
                l.start();
              else
                l.stop();            
            }
          });
        }
      }
    };
  });

@lyadeski
Copy link

lyadeski commented Oct 5, 2014

@ValentinH - nice modification! much better than the previous reliance on the ID

@spongessuck
Copy link

@ValentinH - This is perfect. A gold star for you, sir.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment