问题描述
我是pretty新角度和我使用的火力点作为我的后端。我希望有人能调试这个问题。当我第一次去我页 www.mywebsite.com/#defaultHash 的数据不加载到DOM,它访问另一个哈希链接,回来后虽然做。
I'm pretty new to Angular and I'm using firebase as my backend. I was hoping someone could debug this issue. When I first go to my page www.mywebsite.com/#defaultHash the data doesn't load into the DOM, it does after visiting another hash link and coming back though.
我的控制器是这样的:
/* initialize data */
var fb = new Firebase('https://asdf.firebaseio.com/');
/* set data to automatically update on change */
fb.on('value', function(snapshot) {
var data = snapshot.val();
$scope.propertyConfiguration = data.products;
console.log($scope.propertyConfiguration);
console.log("Data retrieved");
});
/* save data on button submit */
$scope.saveConfigs = function(){
var setFBref = new Firebase('https://asdf.firebaseio.com/products');
setFBref.update($scope.propertyConfiguration);
console.log("configurations saved!");
};
我有3个散列路线说共享,注册和家与otherwise.redirectTo设置为共享(它们都使用该控制器)下面是发生错误:。(所有的通 HREF =#hashWhereever)
I have 3 hash routes say "Shared", "Registration", and "Home" with otherwise.redirectTo set to "Shared".(They all use this controller) Here's the error that occurs: (all "links" are href="#hashWhereever")
1)转到 website.com/#Shared 或只刷新。控制台日志$ scope.propertyConfiguration和数据取自。 DOM显示什么。
1) Go to website.com/#Shared or just refresh. Console logs $scope.propertyConfiguration and "Data Retrieved". DOM shows nothing.
2)单击 website.com/#Registration ,控制台日志$范围的数据正确,DOM加载正确。
2) Click to website.com/#Registration, console logs $scope data properly, DOM is loaded correctly.
3)点击回 website.com/#Shared ,控制台日志$范围数据尚未正确这次DOM正确加载。
3) Click back to website.com/#Shared, console logs $scope data properly yet this time DOM loads correctly.
4)刷新当前加载正确的 website.com/#Shared 。 DOM元素消失了。
4) Refresh currently correctly loaded website.com/#Shared. DOM elements disappear.
由于$ scope.data是在所有的情况下正确这里,不应角确保在DOM反映了模型正确?为什么它的DOM正确加载,只有当我从另一个链接点击页面。
Since $scope.data is correct in all the cases here, shouldn't Angular make sure the DOM reflects the model properly? Why is it that the DOM loads correctly only when I am clicking to the page from another link.
我可以通过添加window.location.hash =共享,但它抛出一个巨大的错误量在控制台修复了。
I can "fix" it by adding window.location.hash = "Shared" but it throws a huge amount of errors in the console.
固定 :(八九不离十)
功能$范围。$适用()强制与模型同步的视图。我回答这个问题,我和关闭它,但我仍然不知道为什么观点并不正确加载,当我正确分配一个值$范围。如果角的脏检查的检查,只要有可能的模式发生了变化,不分配值$范围overqualify?
The function $scope.$apply() forces the view to sync with the model. I'd answer this question myself and close it but I'm still wondering why the view doesn't load correctly when I correctly assign a value to $scope. If Angular's "dirty checking" checks whenever there is a possibility the model has changed, doesn't assigning a value to $scope overqualify?
推荐答案
角有没有办法知道您分配一个值$ scope.variable。这里没有魔法。当你运行一个指令(NG-点击/ NG提交)或角度的内部功能,都调用$适用()并引发摘要(肮脏的标志和更新程序的检查)。
Angular has no way to know you've assigned a value to $scope.variable. There's no magic here. When you run a directive (ng-click/ng-submit) or Angular internal functions, they all call $apply() and trigger a digest (a check of the dirty flags and update routine).
一个超过$可能更安全的方法适用于将使用$超时。目前,如果你调用火力地堡的写操作,它可以同步触发事件侦听器(child_added,child_changed,价值等)。这可能会导致你调用$应用而仍然是一个$内申请范围。如果你这样做,会引发Error。 $超时绕过这一点。
A possibly safer approach than $apply would be to use $timeout. Currently, if you call a write op in Firebase, it could synchronously trigger an event listener (child_added, child_changed, value, etc). This could cause you to call $apply while still within a $apply scope. If you do this, an Error is thrown. $timeout bypasses this.
请参阅了解多一点的话题的消化和$暂停。
See this SO Question for a bit more on the topic of digest and $timeout.
;非常大背景阅读任何严肃的角度开发。
This doc in the Angular Developer Guide covers how compile works; very great background read for any serious Angular dev.
此外,您还可以通过使用官方火力地堡绑定保存自己的能量一个很好的协议,它已经采取了所有这些实现细节考虑在内。
Also, you can save yourself a good deal of energy by using the official Firebase bindings for Angular, which already take all of these implementation details into account.
相关依稀注意:在不太遥远的将来,角将能够采取的来处理这些更新。
Vaguely Related Note: In the not-too-distant future, Angular will be able to take advantage of Object.observe magic to handle these updates.
这篇关于为什么角不与范围在这里更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!