riegersn
riegersn

Reputation: 2849

How can I get ui-router resolve to work when using ng-controller?

Is it possible to get resolves working when using ng-controller? I prefer to use ng-controller as it allows me access to all 1.6+ life-cycle hooks such as $onDestroy, which I loose when defining the controller on state obj.

Plunker: https://plnkr.co/edit/2FJ0dGtFQtBtcQ0uVbTi?p=preview

In the example below, the view loaded in 'main' makes the myData available to inject, however in main2 the controller is defined with ng-controller and myData is no longer available to inject.

$stateProvider.state('home', {
    url: '/',
    views: {
        'main': {
            controller: 'MainCtrl',
            controllerAs: 'vm',
            templateUrl: 'main.html'
        },
        'main2': {
            templateUrl: 'main2.html'
        }
    },
    resolve: {
        myData: function() {
            return ['My', 'resolve', 'is', 'working'];
        }
    }
});

Upvotes: 2

Views: 21

Answers (1)

georgeawg
georgeawg

Reputation: 48968

Instantiated with ui-router state:

app.controller('MainCtrl', function(myData) {
    console.log("MainCtrl")
    console.log(myData);
    console.log(this);
    this.message = myData.join(' ');
});

Instatiated with ng-controller:

app.controller('MainCtrl2', function($scope) {
    console.log("MainCtrl2");
    console.log($scope);
    this.message = $scope.$resolve.myData.join(' ');
});

The DEMO on PLNKR.

Upvotes: 2

Related Questions