Angular.js delaying controller initialization

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 AngularJS route change until model loaded to prevent flicker, but couldn't get it working with Angular 1.1.0


<script type="text/ng-template" id="/editor-tpl.html">
Editor Template {{datasets}}
    <div ng-view>


function MyCtrl($scope) {    
    $scope.datasets = "initial value";

MyCtrl.resolve = {
    datasets : function($q, $http, $location) {
        var deferred = $q.defer();

        //use setTimeout instead of $http.get to simulate waiting for reply from server
            deferred.resolve("updated value");
        }, 2000);

        return deferred.promise;

var myApp = angular.module('myApp', [], function($routeProvider) {
    $routeProvider.when('/', {
        templateUrl: '/editor-tpl.html',
        controller: MyCtrl,
        resolve: MyCtrl.resolve


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);

Need Your Help

Conditional OCR rotation on the image or Page in KOFAX

image-processing vbscript ocr kofax

We have two source of inputs to create a Batch first is Folder Import and second is Email import.

convert pixel values to image in c++

image image-processing

I have a file which has 2 columns. 1st column has all pixel positions and second has value corresponding to that pixel. This pixel value is very small (of order E-9).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.