【问题标题】:angularFire() is returning undefined when the controller is loaded the second time第二次加载控制器时,angularFire() 返回未定义
【发布时间】:2013-04-06 18:21:24
【问题描述】:

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

我的控制器代码是

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'

【问题讨论】:

  • 我尝试调试代码,它指向了这个方法_safeApply。当 fn(); 被调用时,它是未定义的,我是新的 js,所以这是我现在所能做的。等我醒了再看看这个
  • 很好,问题是如果值在本地缓存,则值处理程序会被同步调用。 Thomas 已正确识别问题,我会尽快合并修复!

标签: angularjs firebase


【解决方案1】:

遇到我认为当前的相同行为是angularFire.js中的bug:

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

更新:这个错误现在是fixed。

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

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

【讨论】:

  • 这解决了这个问题,但是当我添加另一个条目时,它会使用不存在的方法 .push 引发错误
  • 它已修复! :) 在我测试之前,我不得不将我的代码恢复为隐式同步进行检查,更改为显式以使一切正常。非常感谢! :) 我对这些东西很陌生,我需要很长时间才能学习。干杯!
猜你喜欢
  • 2019-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-18
  • 1970-01-01
  • 2021-08-14
相关资源
最近更新 更多