0

我是 angularjs 的新手,并且正在使用 firebase 来玩弄它。我正在关注 angularFire隐式同步教程。

我的控制器代码是

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:无法调用未定义的方法“then”

4

1 回答 1

4

遇到相同的行为,我认为目前存在以下错误angularFire.js

如果请求的值已经在本地缓存,则angularFire()返回undefined而不是(已解决的)承诺。

更新:该错误现已修复

关于后续调用的问题$scope.stores.push:我建议区分绑定值(在 angularFire() 的第三个参数中命名)和绑定的承诺(由 angularFire() 返回):

$scope.promise = angularFire(url, $scope, 'stores', []);
$scope.promise.then (function () {
    $scope.stores = { foo: 'bar', ... };
});
于 2013-04-07T17:57:22.460 回答