AngularJS +how to wait for task to finish before running next task -


i have still problems database, found out, problems come fact, opening database takes time, app not waiting till task has finished.

is there way make angular wait till database opened correctly before starts next task?

thanks, christian.


update: 2016-08-08 - 03:13 pm

thanks answers. can see, first idea promises ($q) right, but:

my app has app.js, main file. calls initdb. should open database. should call createtables, creates table, if doensn't exist.

the rest of app splitted in 4 pages (templates). every page has it's own controller.

so idea init db, create table, , work database, used on different controllers.

this won't work, because need put of stuff in .then() of initdb in app.js???

this first angularjs app, maybe reason why lot of mistakes...

thanks, christian.

one of core concepts of angular working services/factories. there ample documentation , blogs how these work , how use them, basic idea these singleton "controllers" handle shared data , methods across entire application. used in combination promises, can create service manage communications database.

angular   .module('myapp')   .service('dbservice', dbservice)   .controller('ctrl1', ctrl1)   .controller('ctrl2', ctrl2)   .controller('ctrl3', ctrl3)   .controller('ctrl4', ctrl4);  dbservice.$inject = ['$q']; function dbservice($q) {   var dbservice = this;   var dbservicedeferred = $q.defer();   dbservice.ready = dbservicedeferred.promise;    // service initialized once, ever run once   (function() {     init();   })();    function init() {     // can use promise chaining control order of events     // chain halt if function rejected     initdb()     .then(createtablesunlessexist)     .then(setdbready);   }    function initdb() {      var deferred = $q.defer();     // simulate async db initialization     $timeout(function() {       deferred.resolve();       // or reject if there error       // deferred.reject();     }, 5000);     return deferred.promise;   };    function createtablesunlessexist() {     //create tables if needed (only happens once)     var deferred = $q.defer();     // simulate async table creation     $timeout(function() {       deferred.resolve();       // or reject if there error       // deferred.reject();     }, 5000);     return deferred.promise;   }    function setdbready() {     dbservicedeferred.resolve();   } } 

now have db setup , don't have worry anymore. can access db controller using service. none of queries run until db has been initialized , tables have been created.

ctrl1.$inject = ['dbservice', '$q']; function ctrl1(dbservice, $q) {   $q.when(dbservice.ready).then(function() {     dbservice.conn.query("select something");   }); } ctrl2.$inject = ['dbservice', '$q']; function ctrl2(dbservice, $q) {   $q.when(dbservice.ready).then(function() {     dbservice.conn.query("select something");   }); } ctrl3.$inject = ['dbservice', '$q']; function ctrl3(dbservice, $q) {   $q.when(dbservice.ready).then(function() {     dbservice.conn.query("select something");   }); } ctrl4.$inject = ['dbservice', '$q']; function ctrl4(dbservice, $q) {   $q.when(dbservice.ready).then(function() {     dbservice.conn.query("select something");   }); } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -