在控制器注入时没有解决承诺

时间:2022-09-04 11:20:45

I'm facing an issue. I'm working with angular 1.2RC3 and ui-route 0.2. If i resolve with a function which is doing a synchronous return, it works. With a promise, the controller is initialized before resolving the promise.

我正面临一个问题。我正在使用角度1.2RC3和ui-route 0.2。如果我使用正在进行同步返回的函数解析,它就可以工作。有了承诺,控制器在解析承诺之前被初始化。

http://plnkr.co/edit/feXHNaGsXwpXDBXkxLZx

angular.module('srcApp', ['ui.router'])
    .config(function ($stateProvider, $urlRouterProvider) {

        $urlRouterProvider.otherwise('/');

        var userResult = {
            id : 'userid',
            displayName : 'displayName'
        };

        var getUserPromise = function ($q, $timeout, $log) {

            var defer = $q.defer;
            $timeout(function () {
                $log.log('promise resolved');
                defer.resolve(userResult);
            }, 2000);
            return defer.promise;
        };

        $stateProvider.state('test', {
            url: '/',
            template: '<div>{{user.displayName}}</div>',
            controller: 'testCtrl',
            resolve : {
              user: getUserPromise
            }
        });
    });


    var testCtrl = angular.module('srcApp').controller('testCtrl', function ($scope, $http, $log, user) {
        $log.log("test controller init");
        $log.log("test controller user=" + user);
        $scope.user = user;
    });

1 个解决方案

#1


2  

Weird... It's pretty much what I do line for line in an app.

很奇怪......这几乎就是我在应用程序中排队的行。

Solved: http://plnkr.co/edit/oC5wK8aDcq82mWl8ES6l?p=preview

You typed:

$q.defer

instead of:

$q.defer()

#1


2  

Weird... It's pretty much what I do line for line in an app.

很奇怪......这几乎就是我在应用程序中排队的行。

Solved: http://plnkr.co/edit/oC5wK8aDcq82mWl8ES6l?p=preview

You typed:

$q.defer

instead of:

$q.defer()