JavaScript Ionic: how to programmatically update ion-toggle

this has been bugging me for ages but I can't figure out how to programmatically change an ion-toggle value.

My toggle starts as on. When someone slides it off, I popup a confirmation. If they cancel the confirmation, I want to reset the toggle back to where it was before:

<ion-toggle ng-model="twitterConnection" ng-change="twitterConnectionChange()">Twitter</ion-toggle>

$ionicPopup.confirm({
                title: 'Are you sure?',
                template: 'Are you sure you want to disconnect this account?'
            }).then(function(res) {
                if(res) {
                    delete $window.localStorage.twitterToken;
                    delete $rootScope.user.twitterID;
                }
                else {
                    $scope.twitterConnection = true;
                }
            });

I've tried wrapping the $scope.twitterConnection = true; line in $apply, but that gives the "digest already in progress" error. I've tried wrapping it in $timeout, but nothing happens. I'm a relative beginner so all help appreciated, thanks!

EDIT: here's a codepen: http://codepen.io/Shiftlemac/pen/pjZRbP

Answer:1

What I recommend you to do, is the following:

Create the function, which will be responsible for changing the value of the toggle.

So, in your case it will be:

$ionicPopup.confirm({
                title: 'Are you sure?',
                template: 'Are you sure you want to disconnect this account?'
            }).then(function(res) {
                if(res) {
                    delete $window.localStorage.twitterToken;
                    delete $rootScope.user.twitterID;
                    $scope.twitterConnectionToggleOn();
                }
                else {
                    $scope.twitterConnectionToggleOff();
                }
            });

and

    $scope.twitterConnectionToggleOff = function(){
        $scope.twitterConnection = false;
    }
    $scope.twitterConnectionToggleOn = function(){
        $scope.twitterConnection = true;
    }
Answer:2

Answer courtesy of the ionic forum:

http://forum.ionicframework.com/t/programmatic-updating-of-ion-toggle-not-working/36155/2?u=shiftlemac

Essentially I needed to use the ng-checked attribute of the ion-toggle and set up the twitterConnection variable as an object as per this kind gentleman's codepen: http://codepen.io/DaDanny/pen/xwJdLr

<ion-toggle ng-model="twitterConnection.checked" ng-change="twitterConnectionChange()" ng-checked="twitterConnection.checked">Twitter</ion-toggle>

$scope.twitterConnection = {
    checked: true,
    text: 'Twitter' }
Answer:3

Good evening everyone, I have an array which contains 24 characters "hours": [ "1", "1", "1", "1", "0", "0", "0", "0", "0", ...

Good evening everyone, I have an array which contains 24 characters "hours": [ "1", "1", "1", "1", "0", "0", "0", "0", "0", ...

  1. replace specific value in pandas dataframe
  2. replace specific value in dataframe r
  3. replace specific value in column pandas
  4. replace specific value in list python
  5. replace specific value in r
  6. replace specific value in array javascript
  7. replace specific value in array php
  8. replace specific value in dataframe python
  9. replace specific value pandas
  10. replace specific value in column r
  11. replace specific value in dataframe
  12. replace specific value with na in r
  13. replace specific value in csv python
  14. replace specific value in string python
  15. replace specific value in excel
  16. replace specific value stata
  17. replace specific value in sql
  18. replace specific value in spotfire
  19. replace specific value in list java
  20. replace specific value in array

I'm using JSX everywhere in my React project because of course I am. But there are a few instances where using createElement is particularly helpful. Here's one such example, which is representative ...

I'm using JSX everywhere in my React project because of course I am. But there are a few instances where using createElement is particularly helpful. Here's one such example, which is representative ...

I have found similar to my issue here. But I have a little bit different scenario. I have string of html rather than just string. So, I wanted to do: Let's suppose MyComponent is just returning h3 ...

I have found similar to my issue here. But I have a little bit different scenario. I have string of html rather than just string. So, I wanted to do: Let's suppose MyComponent is just returning h3 ...

  1. convert react component to html
  2. convert react component to pdf
  3. convert react component to typescript
  4. convert react component to image
  5. convert react component to string
  6. convert react component to vue
  7. convert react component to angular
  8. convert react component to functional component
  9. convert react component to react native
  10. convert react component to web component
  11. convert react component to hooks
  12. convert react component to png
  13. convert react component to json
  14. convert react component to javascript
  15. convert react component to dom element
  16. convert react component to element
  17. convert react component to npm package

I did some research on how to remove logo which removes the Vimeo logo on the bottom right corner. But I want to remove the user logo and author from embedded vimeo video. Is this possible? If yes how ...

I did some research on how to remove logo which removes the Vimeo logo on the bottom right corner. But I want to remove the user logo and author from embedded vimeo video. Is this possible? If yes how ...

  1. vimeo hide user