0

As my app initializes, the call to the api happens:

.run(function($ionicPlatform, $http, $localstorage, $model) {
  $http.get($model.apiurl).success(function(data) {
    $localstorage.setObject('data', data);
    // reload template here!
  });
})

When the api call has succeeded and the localstorage object is set, I want to reload my template (tab-categories.html) so the data can be displayed. How do I do this, ngRoute, stateProvider, ... ?

2 Answers 2

1

You might be missing the point of angular if you ask this question. If your template has values which are bound to a model, then changing those values will automatically update the view on the next digest. It is possible that your asynchronous code (the request) does not trigger a digest, in which case you will have to do it manually. There are many ways to do that: digest and apply

One simple way is to inject $timeout, and do a zero duration timeout (no time argument) with the sensitive code in the body of the function you pass in

Edit: so to answer your question more directly, you should be storing your data somewhere in your application when the call succeeds, and then rely on the angularjs digest loop to update your view. That's one of angulars big work saving features.

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

4 Comments

I understand what this does, but not how I connect both parts. In my app.js in the 'run' function I call my api and set the localstorage object. But how do I let my controller know that the call is ready and the data is ready to reload. Or do I have to do the api call in my controller? Sorry, I'm new to this concept...
You could inject the controller to the relevant view and call a function on it that tells the view to update. Try injecting the controller and log it to see what's inside.
Is $apply() required? Because even when I remove it, the watcher catches all changes.
That depends on the way you did your bindings. $apply will be called for you if a watched variable changes.
0

Use $route.reload(); method to reload entire page after your successful Transaction, be sure to add dependency injection '$route' in your Controller.

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.