1

Based on changing the value of a variable I wish to display an error message in my html. I call an api from my angular code, and if it returns an error, I have set up a setInterval function that should update bookingData.tracking_id to false and then show an error message in the html. This should be very easy but combining this with setInterval is proving slightly difficult.

Here is the angular/javascript

this.confirmTrackingTest = function () {
  TestService.finalPackageCheck({tracking_id: controller.bookingData.tracking_id}).$promise.then(function (data) {
    return data;
  }).catch(function (err) {
    var i = 0;
    var interval = setInterval(function () {
      i += 1;
      if (i === 3) {
        if (err.statusText === "Not Found") {
          controller.bookingData.showErrorMessage = true;
        }
        clearInterval(interval)
      }
    }, 2000);
    console.log(controller.bookingData.showErrorMessage)
  });
}

this.bookingData = {
  showErrorMessage: false,
  tracking_id: 1
};

Here is the html:

{{Packs.bookingData.showErrorMessage}}
<div class="alert alert-danger" role="alert" ng-if="Test.bookingData.showErrorMessage">
  <p>Please show this message</p>
</div>

The {{Packs.bookingData.showErrorMessage}} shows false so that is recongised in the html.

Please let me know if any further information is required.

3
  • By the looks of it, your interval is going to stop after being called once. Commented Sep 17, 2015 at 12:14
  • why do you say that? it gets called 3 times Commented Sep 17, 2015 at 12:16
  • 1
    Ah, bad indenting on the closing brackets.... Back to drinking my coffee Commented Sep 17, 2015 at 12:25

1 Answer 1

9

This is exactly the reason why the Angular gods invented $interval in order to remove the need to manually use $apply.

So either you call $scope.$apply inside the callback or you use $interval (don't forget to include it in the parameter list for depedency injection)

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

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.