Disabling ng:click

3,649 views
Skip to first unread message

rur

unread,
Jul 17, 2011, 11:08:15 AM7/17/11
to angular
I attempted to write a directive that I could use to disable/enable an
ng:click expression from being evaluated but I couldn't get it to
work.

'stopPropagation' didn't do it so I tried adding/removing the
attribute, but still no luck: http://jsfiddle.net/rur_d/CuSeZ/1/

I ended up writing my own basic version of ng:click and handing
support for "my:disabled" within that directive. Seems to work well.
See here: http://jsfiddle.net/rur_d/CuSeZ/3/

Am I missing a trick with the first approach or is a custom click
directive the only option in this case?

Thanks,

rur

Witold Szczerba

unread,
Jul 18, 2011, 3:46:41 AM7/18/11
to ang...@googlegroups.com
I am not sure, but I think that it is not enough just to remove/modify
the attribute in hope angular interprets it all the time. Markup must
be compiled/linked, so ng:click, once it is interpreted by angular,
will not change it's behavior only because you have modified the
attribute. You would have to recompile the node, but don't as me how
:)
Consider another solution: bind click with controller function and
inside that function make a decision to do something or not.

Regards,
Witold Szczerba

P.S.
scope.$get in the snippet below:
scope.$watch(exp, function() {
if (scope.$get(exp)) { ... }
}
can be replaced like this:
scope.$watch(exp, function(newValue, oldValue) {
if (newValue) { ... }
}

> --
> You received this message because you are subscribed to the Google Groups "angular" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to angular+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/angular?hl=en.
>
>

Vojta Jina

unread,
Jul 18, 2011, 10:32:39 AM7/18/11
to ang...@googlegroups.com
Hey rur - you are right, implementing this in "my:disabled" would be tricky. Right place for that is inside "ng:click", so your idea is right - creating custom directive.
I feel like this should be part of angular - ng:click should check for ng:disabled, don't you think ?
Problem might be when you need to call ng:click even for disabled element...
V.

Vojta Jina

unread,
Jul 18, 2011, 10:39:28 AM7/18/11
to ang...@googlegroups.com
Actually, this could be related to https://github.com/angular/angular.js/issues/259

If we decide to go for solution like this:
<a href ng:click=".... here is $event ...."></a>, so that you can use:
<a href ng:click="controllerMethod($event, otherParams)"></a>

This information (ng:disabled) could be part of our event wrapper object ($event) then...
Just a thought...
V.

Misko Hevery

unread,
Jul 18, 2011, 12:42:36 PM7/18/11
to ang...@googlegroups.com
I don't see why this should be part of the markup. You are asking to pub business logic into the view, which is not right. I think what you want is to have your controller either take action or not based on some internal state. 

-- Misko

V.

--
You received this message because you are subscribed to the Google Groups "angular" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/H4XT7kFGj9wJ.

rur

unread,
Jul 18, 2011, 4:23:39 PM7/18/11
to angular
Thank for the tip regarding the callback params :)

I don't really see how ng:disabled on a clickable item is any
different to something like ng:hide. The reason I want to use it is to
reflect the state and to avoid more view specific logic in the
controller.

To be honest I'm finding it hard to draw a clear line between what
logic belongs in the markup vrs controller/widget/directive/service.
On the whole I have a fair idea, but some of the finer points are
difficult to grasp. Would it be worth looking to Flex conventions for
best practice?

rur

Misko Hevery

unread,
Jul 18, 2011, 7:40:41 PM7/18/11
to ang...@googlegroups.com
Disabling a button makes sense it is gray, but what does it mean to disable a link? The UI, looks same it is just that it does not do anything.

-- Misko

Vojta Jina

unread,
Jul 19, 2011, 8:12:47 AM7/19/11
to ang...@googlegroups.com
Misko is right. When using ng:disabled or my:disabled - it's binded to some model... So let's check this model, you don't need touching the UI at all..
That's much better than my previous suggestion...

See your example updated: http://jsfiddle.net/CuSeZ/9/

V.
Reply all
Reply to author
Forward
0 new messages