1

Here is what i expect to happen, i am using anulgarjs as controller

1.when a button is clicked, call the http post function and return a boolean value

2.the returned boolean is a checking condition

3.if ture, attach a url to a html element of href attribute , and clicked immediately

4.if false, a model pop up

$scope.checking = function(arg){
  $http.post('path/').success(function(r){
     // r is a boolean value 
    deferral.resolve(r);
  });
  return deferral.promise;
};

second function

$scope.secondfun =function(){
  url= 'someurl'
  $scope.checking().then(function(r){
    if(r){
      $('#htmlelement').attr('href', url);
    }
    else{
      $('#somemodal').modal('toggle');
    }
  });
}

problem: the order is not as i expected model or url don't show up right after the button clicked. Instead, it shows up when i clicked the button again

Meanwhile , as i am not sure i've used the promise correctly , i tried something like this on the second function :

$scope.secondfun = function(){
  url= 'someurl';
  var bool = false;
  $scope.checking().then(function(r){
    bool = r;
  });
  if(bool){
    $('#htmlelement').attr('href', url);
  }
  else{
    $('#somemodal').modal('toggle');
  }
};

problem: the if clause execute before the bool is assigned , bool is undefined

So , i just need something help to solve the problem

3
  • In your checking function, return $http.post('path/') should do the trick Commented Nov 28, 2016 at 16:08
  • I believe tried to just return the http promise, but the problem still exist , the URL or model just not show up immediately and when I click it again it show up , so seem the html attribute updated but not execute on the same click event Commented Nov 29, 2016 at 5:12
  • It seems I can use a document.getelementbyid and call click function to solve this , but any suggestions that it won't work like I expected Commented Nov 29, 2016 at 5:17

2 Answers 2

3

Try changing your checking function to this

$scope.checking = function(arg){

return $http.post('path/')
  .then(function(r) {
    return r;
  },function(error){
    return error;
  });

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

Comments

3

Just return the promise when checking

$scope.checking = function(arg){
  return $http.post('path/');
}

And then use that promise as you are doing already

$scope.secondfun = function(){
  url= 'someurl'
  $scope.checking().then(function(r){
    if(r){
      $('#htmlelement').attr('href', url);
    }else{
      $('#somemodal').modal('toggle');
    }
  });
}

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.