Created
June 5, 2013 20:50
-
-
Save eperedo/5717222 to your computer and use it in GitHub Desktop.
AngularJs Directive for ladda buttons - https://github.com/hakimel/Ladda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = angular.module('plunker', []); | |
app.controller('MainCtrl', function($scope, $timeout) { | |
$scope.save = function(){ | |
$scope.loading = true; | |
$timeout(function(){ | |
$scope.loading = false; | |
}, 3000); | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
}); | |
} | |
}; | |
}); |
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();
}
});
}
}
};
});
@ValentinH - nice modification! much better than the previous reliance on the ID
@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
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:
(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: