【问题标题】:angularFire route resolutionangularFire路由分辨率
【发布时间】:2013-04-06 06:29:49
【问题描述】:

我想让我的angularFire 集合解析路由加载。比如:

App.config ($routeProvider, angularFireProvider) ->
  $routeProvider.when '/test',
    templateUrl: 'views/test.html'
    controller: 'TestCtrl'
    resolve: angularFireProvider.resolve 'testItems'

可以这样做吗?

【问题讨论】:

    标签: javascript angularjs firebase


    【解决方案1】:

    我不确定为什么您希望集合在路由加载时解决,而不是在控制器中解决 - 您能详细说明一下吗?例如,以下内容也可以:

    App.config ($routeProvider, angularFireProvider) ->
      $routeProvider.when '/test',
      controller: 'TestCtrl'
    
    function TestCtrl($scope, angularFire) {
      angularFire("https://<example>.firebaseio.com", $scope, "collection").
        then(function() {
          // The collection has been resolved and available in $scope.collection
        });
    }
    

    主要是语法方便还是我缺少上面想要的功能?

    更新:在$routeChangeSuccess 事件被触发之前要解析的值:

     App.config(['$routeProvider', 'angularFire', function($routeProvider, angularFire) {
       $routeProvider.when("/test", {
         templateUrl: 'views/test.html'
         controller: 'TestCtrl',
         resolve: {collection: angularFire("https://<example>.firebaseio.com")}
       });
     }]);
    
     function TestCtrl(collection) {
       // collection has already been resolved to its value.
     }
    

    【讨论】:

    • 在这个方法中$routeChangeSuccess会在angularFire资源被解析之前发出,对吧?
    • 你是对的 - 如果你想在事件触发之前解析值,你可以简单地将调用 angularFire 移动到你的解析属性中,因为它返回一个承诺。我将编辑答案以提供代码 sn-p。
    • Anant,如果您可能会看到这一点,我正在尝试使用解析功能,但它指出 angularFire 是一个未知的提供者(在相关说明中 angularFireCollection 在这种情况下不更合适由于不需要范围?)。 aF 和 aFC 在我的代码中的其他地方工作正常,但是在将它与 config 和 resolve 一起使用时可能缺少什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    相关资源
    最近更新 更多