Angular.js delaying controller initialization

后端 未结 3 1044
情深已故
情深已故 2020-12-03 05:28

I would like to delay the initialization of a controller until the necessary data has arrived from the server.

I found this solution for Angular 1.0.1: Delaying Angu

3条回答
  •  遥遥无期
    2020-12-03 06:14

    Since $http returns a promise, it's a performance hit to create your own deferred just to return the promise when the http data arrives. You should be able to do:

    MyCtrl.resolve = {
        datasets: function ($http) {
            return $http({method: 'GET', url: '/someUrl'});
        }
    };
    

    If you need to do some processing of the result, use .then, and your promise is chained in for free:

    MyCtrl.resolve = {
        datasets: function ($http) {
            return $http({method: 'GET', url: '/someUrl'})
                   .then (function (data) {
                       return frob (data);
                   });
        }
    };
    

提交回复
热议问题