Disable toggle switch

707 views
Skip to first unread message

Karan

unread,
Mar 10, 2015, 7:13:31 AM3/10/15
to ang...@googlegroups.com
Hi,

(using angular and typescript)
I have a toggle switch directive as:

return {
restrict: 'E',
scope: {
ngModel: '=',
onText: '@',
offText: '@',
title: '@',
},
template:
'<label class="switch-light well">' +
'  <input type="checkbox" ng-model="ngModel">' +
'  <span>' +
'    {{title}}' +
'    <span class="switch-light-option" ng-class="{ \'switch-light-checked\': !ngModel}">{{offText}}</span>' +
'    <span class="switch-light-option" ng-class="{ \'switch-light-checked\': ngModel}">{{onText}}</span>' +
'  </span>' +
'  <a class="btn btn-primary"></a>' +
'</label>'
};

In my html I am using this as:
<toggle-switch ng-model="myValues" on-text="Enabled" off-text="Disabled" ></sts-toggle-switch>

Now I want to disable the above one. How can I do so. 

I am pretty new to angular so any inputs would be appreciated.

Thanks

KJ

unread,
Mar 12, 2015, 4:26:45 AM3/12/15
to ang...@googlegroups.com
Anyone??

Sander Elias

unread,
Mar 12, 2015, 6:54:23 AM3/12/15
to ang...@googlegroups.com
Hi KJ,

You will have better luck getting answers when you build us a plunk demonstrating your issue.
If you want to disable this, you have to factor in ng-disabled. However, I see some issues here. it's easy to disable the checkbox, But what are you going to do with your label and your span thingies?

Regards
Sander

KJ

unread,
Mar 12, 2015, 1:32:14 PM3/12/15
to ang...@googlegroups.com
Thanks.

I have created the plunker at:
 
All info of directive, html, css and script is there.
So when some click either yes/no I want to disable the entire one for sometime till we get the data from the db.

Any inputs ..thanks

Sander Elias

unread,
Mar 12, 2015, 9:58:17 PM3/12/15
to ang...@googlegroups.com
Hi KJ,

Just add in the disabled switch. have a look at this: http://plnkr.co/edit/wFLNxoAWRI39kiInzBWv?p=preview
However, I could not find a way to let your css component show to the user that it is disabled. This is something you have to take care of. The disabled is working, however, the user is clueless that it is indeed disabled.
There are many ways to take care of that. I don't know what will fit in your surroundings. using ngHide and ngShow to show just the text in stead of the input replacement would suffer in a lot of cases I suppose.

Regards
Sander

KJ

unread,
Mar 13, 2015, 5:57:01 AM3/13/15
to ang...@googlegroups.com
Thanks Sander. With you code the disabled part works fine as once disabled one cannot click. But if you add disabled='false' then it still remains disabled.

Sander Elias

unread,
Mar 13, 2015, 7:37:22 AM3/13/15
to ang...@googlegroups.com

If KJ,

If you had tried it with an empty value, you would have seen that it worked. In your private scope object change disabled: '@' to disabled: '=',, and it will work.

Do you understand why?

Regards
Sander

KJ

unread,
Mar 13, 2015, 10:45:57 AM3/13/15
to ang...@googlegroups.com
Ok I understand. I think '=' is for two way binding and '@' is for one way.

I would try to work on the css part here and see if I can get that through.
Reply all
Reply to author
Forward
0 new messages