2013-04-06 53 views
0

我是angularjs的新成員,並且正在與firebase一起玩耍。我遵循隱含同步的 angularFire tutorial當第二次加載控制器時,angularFire()返回undefined

我的控制器代碼是

trankeeloManagerApp.controller('StoresCtrl', [ 
    'NavService', '$scope', 'angularFire', 
    function(NavService, $scope, angularFire) { 
    NavService.updateActiveNav(); 

    var url = '[MY_URL]/users/test_user/stores'; 
    $scope.stores = angularFire(url, $scope, 'stores', []); 
    $scope.store = { 
    name: '', 
    tin: '', 
    description: '' 
    }; 

    $scope.page = { 
    show_list_loading : true, 
    show_list_table : false, 
    show_list : true, 
    show_add_item : false, 
    show_add_button : false, 
    }; 

    $scope.stores.then(function(stores) { 
    $scope.hideList(); 
    console.log(stores); 

    $('#datatables').dataTable({ 
     "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>", 
     "sPaginationType": "bootstrap", 
     "oLanguage": { 
       "sLengthMenu": "_MENU_ records per page" 
     }, 
     "aaData" : $scope.stores, 
     "aoColumns": [ 
      { "sTitle": "Name", "mData": "name", "mDataProp" : "name" }, 
      { "sTitle": "TIN", "mData": "tin", "mDataProp" : "tin" }, 
      { "sTitle": "Description", "mData": "description", "mDataProp" : "description" } 
     ] 
    }); 
    $scope.page.show_add_button = true; 
    $scope.addStore = function(){ 
     $scope.showList(); 
     $scope.stores.push($scope.store); 
     addStoreInDatatable($scope.store); 
     $scope.store = { 
     name: '', 
     tin: '', 
     description: '' 
     }; 
    }; 

    $scope.removeStore = function (store) { 
     $scope.stores.splice($scope.stores.indexOf(store), 1); 
    }; 

    //TODO implement edit 
    }); 

    $scope.showAddStore = function(){ 
    $scope.page.show_add_item = true; 
    $scope.page.show_list = false; 
    } 

    $scope.cancelAddStore = function(){ 
    $scope.hideList(); 
    } 

    $scope.showList = function(){ 
    $scope.page.show_add_item = false; 
    $scope.page.show_list = true; 
    }; 

    $scope.hideList = function(){ 
    $scope.page.show_list_loading = false; 
    $scope.page.show_list_table = true; 
    }; 

    function addStoreInDatatable(store){ 
    $('#datatables').dataTable().fnAddData({ 
     "name" : store.name, 
     "tin" : store.tin, 
     "description" : store.description 
    }); 
    }; 

    $scope.showList(); 

}]); 

一切運作良好的第一負載,但是當我去到另一個控制器並返回。我得到這個錯誤。

TypeError: Cannot call method 'then' of undefined

+0

我試着調試代碼,它指向這個方法'_safeApply'。 當'fn();'被調用時,它是未定義的,我對新的js,所以這是儘可能我現在可以去。當我醒來時,我會盡量多看看這個 – 2013-04-06 23:27:13

+0

好的問題是,如果值本地高速緩存,值處理程序將被同步調用。托馬斯已經正確識別了這個問題,我很快就會合並修復! – Anant 2013-04-08 06:56:18

回答

4

遇到了相同的問題,我認爲有 目前 bugangularFire.js

如果請求的值已經在本地緩存,然後angularFire()不會返回undefined,而不是(解決)的承諾。

更新:該錯誤現在是fixed

關於與後續調用您的問題$scope.stores.push:我建議綁定的值(在第三個參數命名angularFire())和承諾來區分的結合(由angularFire()返回):

$scope.promise = angularFire(url, $scope, 'stores', []); 
$scope.promise.then (function() { 
    $scope.stores = { foo: 'bar', ... }; 
}); 
+0

這解決了這個問題,但是當我添加另一個條目時,它會拋出一個錯誤,使用一個不存在的方法'.push' – 2013-04-08 10:38:25

+0

它的固定! :)我不得不將我的代碼恢復爲隱式同步來檢查,在我測試之前將其更改爲顯式以使所有內容都可用。非常感謝! :)我對這個東西很陌生,需要花很長時間才能學習。乾杯! – 2013-04-08 19:26:39

相關問題