【发布时间】:2016-10-14 06:17:46
【问题描述】:
我正在使用 angularFire 和 Angular 来更新一些视图,但奇怪的是当我从视图切换到视图时数据不会加载,但当我刷新页面时它会加载。怎么回事?
向导控制器:
/* initialize data */
var ref = new Firebase('https://dlwj.firebaseio.com/');
/* set data to automatically update on change */
$scope.currentLocation = $route.current.$$route.originalPath;
$scope.propertyRef = $firebase(ref);
$scope.propertyRef.$on('loaded', function(value) {
//value will be undefined when I switch views using hash routes.
//there is no issue when I just refresh on that page.
console.log(value);
$scope.propertyConfiguration = value.products;
var products = [];
for (var key in value.products) {
if (value.products.hasOwnProperty(key)) {
products.push(key);
}
}
$scope.productsArray = products;
});
console.log('Data retrieved');
路线:
$routeProvider.when('/SharedProperties',
{
templateUrl: 'partials/SharedPropertiesPartial.html',
controller: 'WizardController'
});
$routeProvider.when('/Registration',
{
templateUrl: 'partials/Registration.html',
controller: 'WizardController'
});
$routeProvider.when('/Login',
{
templateUrl: 'partials/Login.html',
controller: 'WizardController'
});
【问题讨论】:
-
这是什么版本的angularFire?为什么将值推入“已加载”内部的数组中,而不是让 $firebase 处理同步到 $scope.products?为什么我们使用数组?如果将 console.log 放在加载的回调中会发生什么;它只会在重新加载时触发而不是查看开关,对吧?
-
版本为0.6.0。我不确定 $firebase 处理同步是什么意思,但我使用 firebase 作为静态数据源,所以我不希望 productsArray 改变。 'loaded' 回调中的 console.log() 始终运行,它要么在 view2view 开关上输出 undefined,要么在页面刷新时输出实际数据。
-
尝试将console.log放入加载的回调中(目前在该方法之外)。
-
我可能理解错了,但会不会像
$scope.propertyRef.$on('loaded', function(){console.log()});一样? B/c 那里已经有一个了。
标签: angularjs firebase angularfire