$routeprovider,使用延迟变量解析

$routeprovider, resolve with deferred variable

我试图仅在 dbReadyDeferred 解决后加载此视图。

dbReadyDeferred 在其他地方定义(JQuery 可用)在 common.js:

var dbReadyDeferred = $.Deferred();

view1.js:

'use strict';

angular.module('myApp.view1', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/view1', {
    templateUrl: 'view1/view1.html',
    controller: 'View1Ctrl',
    resolve: dbReadyDeferred.promise
  });
}])

.controller('View1Ctrl', ['$scope', function ($scope) {
...

但视图未加载。你能帮忙吗?

我不确定你的解决方案出了什么问题,但我为你做了一个有效的 fiddle:

  $stateProvider.state('home', {
    url: "",
    views: {
        'main': {
            template: '<div>go! {{message}}</div>',
            controller: function ($scope) {
                $scope.msg = "loaded main!";
                console.log("ctrl here!");
            }
            }
    },
    resolve: {
      app: function ($q, myService) {
        var deferred = myService.getDeferred();
        return deferred.promise;
      }
}
});

https://jsfiddle.net/zn8xdLnw/2/

也许你的延迟没有得到解决?

好的,这对我有用:

  $routeProvider.when('/view1', {
    templateUrl: 'view1/view1.html',
    controller: 'View1Ctrl',
    resolve: {
         dbState: ['$rootScope', '$q', function($rootScope, $q) {
               var promise = $q.when(dbReadyDeferred);
               promise.then(function() {
                   $rootScope.$broadcast("dbStateReady");
               });
               return promise;
         }]
    }
  });