So this is a quick, not too serious post about a problem I ran into a couple days ago that I think is worth writing down.
I am a huge fan of Twitter Bootstrap as I think it gives you so much to work with right out of the gates. I hit a snag a couple days ago when I was trying to apply ng-disabled to an anchor tag.
In Twitter Bootstrap it is super easy to apply their .btn class to just about anything. Their docs give me permission so why wouldn’t I!?
Button styles can be applied to anything with the .btn class applied. However, typically you’ll want to apply these to only <a> and <button> elements for the best rendering.
So when I applied ng-disabled=”true” to my freshly minted button it would fade out but I was still able to click through. Ummmmm… what the deal?
So here is the story on why this happens. Form controls have a disabled property on them but anchor tags do not. Therefore, ng-disabled has nothing to actually pin itself on.
So the fix is super easy once I figured out what was going on.
This
<a class="btn btn-primary" ng-disabled="checked" ng-click="handleClick()">ANCHOR</a>
became this
<button class="btn btn-primary" ng-disabled="checked" ng-click="handleClick()">BUTTON</button>
And bam! Disaster averted. And by ‘disaster averted’ I mean I got this fixed before QA busted me.
Here is a more comprehensive fiddle illustrating my experience.
I hit this same issue some weeks ago and it made me spend two hours figuring why it didn’t work.
So am I am NOT the only one! 😀
I know you already know about this, but I just wanted to post the link to the AngularJS issues for people who find this page.
https://github.com/angular/angular.js/issues/1651
https://github.com/angular/angular.js/issues/1674
https://github.com/angular/angular.js/issues/1662
Good lookin’ out homey! I was about ready to kill a rock trying to figure out why ng-click was still firing when ng-disabled was set to true!
Waw! Exactly what I needed!
Thanks. Just what I was looking for (after wasting 10 min debugging in a completely wrong place).
Thanks!
To move from an anchor tag to a button wouldn’t you have to write a function to change views rather than relying on Angular’s built in routing?
Is that frowned upon?
This case came up because I was actually using an anchor tag as a button in a form since Bootstrap makes those controls interchangeable.
If I actually needed anchor tag functionality, I would approach the problem a little different. More than likely I would show or hide the anchor tag instead of trying to disable it.
You’re an idiot.
Give proper solutions not workarounds! There are elements, CSS, logic, etc that rely on links being used, not buttons.
First of all – I think you’re rhetoric is indicative of a total lack of manners which is a larger problem than the scope of my blogpost.
Secondly, I think you are a smart individual that can discern the cases where a button is not an appropriate solution.
Thirdly, this is the ‘workaround’ that was pointed out to me by Igor after talking it over with him. I am more than happy to let him know that he is an idiot on your behalf if you would like as well.
So I’m running into this issue as well. However, my anchor tags are within a dropdown menu inside of elements. I’d prefer to keep them as a link and not a button. Any ideas?
Sure you could do it with a directive and swallow the event or if you want to live life on the edge… with css and pointer-events: none; Hope that helps!
Ha ha ha.
Thanks pal. Your post saved me too from getting busted.
Nice trick !
I also encountered the same problem… I applied a CSS class to the anchor tag
data-ng-class=”{disabled: !canCreate}”
that anchor tag will be disabled if canCreate is false from the controller!
This is a dumb hack, now your anchor looks like a button…
From the Bootstrap docs…
I want to keep my anchor tag in disable state and do not want to make it as button…. Given fiddle link is showing that ng-disabled does not work with anchor tag.
So any idea?
An suitable alternative I have found to retain the anchor element type (if you require an A tag, or are using href/ng-href) is to include the same logic as ng-disabled in a ng-click to conditionally prevent the default click action, as follows: Link text here
With ng-click, you could also change it to ng-click=”checked || handleClick()”. If it is ng-href, you could do ng-ref=”{{checked ? ” : ‘your URL’}}”.
Thanks!
disabled attributes don’t apply to anchors, only to buttons. Interestingly the disabled class is obeyed by ng-click in angular 1.3
http://plnkr.co/edit/lDEUKcqVZ663BRgYFr2C?p=preview
Thats all very well, but what you have now is a button and not an anchor. They dont look at all the same.
There should be a way to make it still look like an anchor. I guess it could be done in styling, so that the border is removed
This is a winning post!
If for whatever reason you do need to keep as an anchor rather than button, could you do this..
ANCHOR
In the “or”, the “checked” is evaluated first and if true evaluation stops so the function is never called.