0

Maybe I'm just not thinking straight. Can I enable/disable a link via my controller using some form of angular ng-click and ng-disabled?

Fiddle here

see fiddle...

The 'Do stuff to things' button should not be enabled unless at least one 'thing' is checked, and no action should be taken either. Of course, once a Thing IS checked, the button should activate.

The more I try to fix this, the more complicated it gets, and I can't help but think I'm overlooking something obvious.

4 Answers 4

3

Because ng-disabled for INPUT's.

Use ng-class here is an example in fiddle

  <a ng-class="{'disabled': things.length==0}" ng-click="doStuff()">Do stuff to Thing(s)</a>

 $scope.doStuff = function(){
        if($scope.things.length==0) return;
        console.log('Stuff has been done to things');
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I can get it to do it once, but when I return to the page, the button is enabled and the check boxes have no effect.
@NarekMamikonyan ngDisabled works with button as well. docs.angularjs.org/api/ng/directive/ngDisabled
1

First of all, you need to remove the curly around your test if you want to do something with ng-disabled. Generally, you dont need to use them with the native angular directive (except for ng-href..)

Afterward, the ng-disabled dont prevent the ng-click if it's not on a button, so you need to use a button or use a syntaxe like this in your ng-click:

ng-click="condition && doSomething()"

Here is a working example, hope it will help you

http://jsfiddle.net/dxquye40/8/

2 Comments

I.do.not. understand angular's curly bracket syntax. Sometimes it's double, sometimes it's single, sometimes it's none at all. I don't know what any of them mean.
it's pretty simple :). The double curly braces declares to angular to interpolates the content as javascript. You can define variables, call functions ... In a directive you dont need to use them because inside the $parse service is called. The content will be interpolate each time the directive need. Moreover the somple curly brace is used to define a javascript object. when you declare ng-class="{active: condition}" you give an literal object with an active attribute.
0

Check out ryeballer's answer in this post: Angular JS + Button should be enabled if at least one checkbox is checked how To do it?

The plunker ryeballer provided should be modified by flipping the condition in the ng-disabled directive:

ng-disabled="!isChecked()"

Here's an adjusted plunker: http://plnkr.co/edit/N0az5UeVH0VRlGK1ZwvC?p=preview

This seems very similar to what you want to do.

Keep in mind also that the ng-disabled directive doesn't seem to affect anchor tags. So, if you can, use something else (such as a button element ). Also, note how your initial solution does not take advantage of Angulars data-binding. Rather than setting ng-click events on your checkboxes, just tell Angular what this data is via the ng-model directive. Let Angular take care of the stuff you've got in your setThing() method.

Comments

-1

If you're allowed to use jQuery then you can use .removeClass(), as I did here with your script.

http://jsfiddle.net/kirdua/dxquye40/21/

code

1 Comment

It is not considered good practice to manipulate the DOM within Angular controllers. The only place it is appropriate is within a directive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.