0

(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" 

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

3 Answers 3

1

Use:

<toggle-switch
   ng-model="switchStatus"
   is-disabled="true">
</toggle-switch>

http://jumplink.github.io/angular-toggle-switch/

Sign up to request clarification or add additional context in comments.

Comments

0

try adding the link code to your directive :

link: function ($scope, element, attrs) {
           $scope.somethingDisabled = true;
        };

and in your html part ng-disabled = somethingDisabled

then all you have to do is just add to the link function how and when you want to disabled either by passing it as an directive attr value or in the controller scope.

makes sense?

EDIT2

as per request I did something that would just show a proof of concept... here is the plunker.. plunker

 link: function ($scope) {
$scope.myclick = function(elem){
 setTimeout(function(){$scope.disabled = true;},0);
  setTimeout(function(){
    $scope.disabled=false;
    alert('db call done');
  },2000);
};

} I fixed the buggyness code might still be kinda ugly but Ill leave it to you to prettify it... the second timeout is the db call... first on is pushing the disable to the end of the stack so you'll see the animation.

if you want you can also just use !ngModel in the ng-disabled and then change the value of the ngModel when the db call come back..

good luck

8 Comments

Thanks for your reply. Basically when I toggle b/w enable and disable by clicking it makes makes a database call and gets some data. At this point I want to disable this control so that we dont keep on toggling again and again.
so wouldnt it be best to start it from the directive meaning ... the rest/db call is probably a factory/service.... so instead of link you can set a controller in the directive that calls the db call and in return changes the scope value thats in the directive... what Im saying is if you do it and you want it generic, wrap it all under the directive...
actually how I have designed our project is that there is not call from any directive. All calls to db are made from service itself. Is there any other way to do this. Someone had mentioned that we can make use of css and some changes to current directive to make this to work.
we are talking about disabling an html attribute right? if so just manually in your controller add the attribute ... not in css but I guess something like angular.element('myElement').attr('disabled','disabled');... again... its not good practice to manipulate DOM from services... thats what controllers and directives are for
sorry jony I am not able to work this on. Would you be able to provide some sample as in how to get this work. THanks
|
0

I have the same issue and I used disabled attribute:

<toggle-switch  label-text="'Enable Internet Cash'" label-position="'top-center'" disabled="MyBankAccount.HasMoney"></toggle-switch>

the disabled attribute receive a Boolean expression. If the disabled attribute has true value it will disable the toogle-switch , in other case it will enable the toggle-switch.

consider that in js file there is $scope.MyBankAccount and $scope.MyBankAccount is json object with HasMoney property.

Comments

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.