【发布时间】: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 已正确识别问题,我会尽快合并修复!