0

I want to revert the selected option in a select to the earlier selected value. On select of an option, I get response from a server call which is resolved via a promise. Based on the response from server, I want to allow/disallow user's selection of currently selected option.

    app.controller('MainCtrl', function($scope, $q, $timeout) {
  $scope.options = [{value: 'abc'},{value: 'def'}];    
    $scope.select = undefined;
    var oldSelect;
    $scope.confirmChange = function() {
        console.log(arguments);
        confirmDialog($scope.select).then(function(val) {
              console.log(val);
                oldSelect = $scope.select;
             },
             function() {
                $timeout(function() {
                  $scope.select = oldSelect;
                }, 10);
            });
    }


    var confirmDialog = function(newVal) {
    // obviously not a good way to ask for the user to confirm
    // replace this with a non blocking dialog

    //the timeout is only for the confirm since it's blocking the angular $digest
    var def = $q.defer();

    c = confirm('Is it ok? [' + newVal.value + ']');
    if(c) {
        def.resolve(true);
    }
    else {
        def.resolve(false);
    }


    return def.promise;
  }
});

I have tried following the solution as mentioned here (SO post) but no use.

I have posted my code on plunkr

Any ideas/pointers would be really helpful. I have spent 4-5 hours searching for a solution on this.

thanks.

Community
  • 1
  • 1
saarthak
  • 1,004
  • 2
  • 12
  • 26

1 Answers1

2

use def.reject(false); instead of def.resolve(false);

khizar naeem
  • 296
  • 2
  • 6