0
 var testApp = angular.module('testApp', ['firebase'])
 .config(['$routeProvider','$locationProvider',function
     ($routeProvider,$locationProvider)
 {

 $routeProvider
  .when('/', {
    templateUrl: '/views/main.html',
    controller: 'MainCtrl'
  })
 .when('/test', {
    templateUrl: '/views/test.html',
    controller: testCrtl,
    resolve:
    {
        firedata: function($q,angularFire){
        var deffered = $q.defer();

        var ref = new Firebase('https://shadowfax.firebaseio.com/items');
        ref.on('value', function(result){
            deffered.resolve(result.val());
        });

        return deffered.promise;
        }

    }
  })
  .otherwise({
    redirectTo: '/'
  });
  // $locationProvider.html5Mode( true );
  }]);


angular.module('testApp')
.controller('MainCtrl', ['$scope','$routeParams','$rootScope', function ($scope,$routeParams,$rootScope) {

    $scope.load = function(){ return false;}
    $rootScope.$on('$routeChangeStart', function(next, current) { 
    $scope.load = function(){ return true;}

    });


}]);

 testApp.controller('TestCtrl',['$scope','$timeout','Fire','firedata','testCrtl']);

 var testCrtl = function ($scope,$timeout,Fire,firedata) {

            $scope.items=firedata;

 };

在上面的代码中,为什么值为$scope.items=firedata;null?请解释如何执行类似 Google 的路由更改来为控制器预加载数据?这个例子就像 John Lindquist 解释的那样工作,但是当我使用 Firebase 的原生 JS 库时,我无法预加载数据。

此外,使用 Firebase angularFire 库也无济于事,因为它使用 $scope 作为参数,并且无法将 $scope 传递给 resolve 函数。

4

1 回答 1

3

您应该能够用于angularFireCollection预加载数据:

.when('/test', {
  templateUrl: '/views/test.html',
  controller: testCrtl,
  resolve: {
    firedata: function(angularFireCollection){
      return angularFireCollection('https://shadowfax.firebaseio.com/items');
    }
  }
})
于 2013-07-16T18:03:53.027 回答