Skip to content

Instantly share code, notes, and snippets.

@Yukilas
Created October 30, 2012 09:42
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yukilas/3979293 to your computer and use it in GitHub Desktop.
Save Yukilas/3979293 to your computer and use it in GitHub Desktop.
AngularJS - Button loading directive
/*
``btn-loading`` attribute.
This attribute will update the button state using Twitter Bootstrap button plugin and
according the attribute value.
The attribute value should be a scope variable.
If the variable is ``true`` the button will have the ``loading`` state.
If the variable is ``false`` the button will be reset and displayed normaly.
Usage:
<button class="btn" btn-loading="is_loading" data-loading-text="Save in progess ..">Save</button>
*/
app.directive("btnLoading", function(){
return function(scope, element, attrs){
scope.$watch(function(){ return scope.$eval(attrs.btnLoading); }, function(loading){
if(loading) return element.button("loading");
element.button("reset");
});
}
});
@battlecow
Copy link

I edited this code to watch for changes in form's ng-disabled state to not interfere with the btn loading, otherwise the button is enabled and the ng-disabled state is ignored.

..directive("btnLoading", function() {
return function(scope, element, attrs) {
scope.$watch(function() {
return scope.$eval(attrs.ngDisabled);
}, function(newVal) {
//you can make the following line more robust
if (newVal) {
return;
} else {
return scope.$watch(function() {
return scope.$eval(attrs.btnLoading);
},
function(loading) {
if (loading)
return element.button("loading");
element.button("reset");
});
}
});
};
})

@elsh32
Copy link

elsh32 commented Aug 19, 2017

drrddd

@elsh32
Copy link

elsh32 commented Aug 19, 2017

ddfff

@elsh32
Copy link

elsh32 commented Aug 19, 2017

Sorry. Cool

@elsh32
Copy link

elsh32 commented Aug 19, 2017

Thanks

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