【发布时间】:2017-02-17 10:47:02
【问题描述】:
我在 Javascript 中遇到错误,因为我的 $digest 循环显然都是同时运行的。
我得到的错误:Error: [$rootScope:inprog] $digest already in progress
我想知道我是否可以使用某些东西来停止这些循环,或者我是否没有正确定义我的功能?
这是我的控制器的代码,你可以看到有两个不同的 $scope 函数,第二个似乎是问题:
.controller('accountController',['$scope', '$firebaseArray', 'CONFIG', '$document', '$state', function($scope, $firebaseArray, CONFIG, $document, $state) {
// Create a reference to the file we want to download
var userId = firebase.auth().currentUser.uid;
console.log(userId);
var database = firebase.database().ref('/accounts/' + userId);
var storageRef = firebase.storage();
var pathReference = storageRef.ref('/' + userId + 'profilepic.jpg');
// Get the download URL
pathReference.getDownloadURL().then(function(url) {
// Insert url into an <img> tag to "download"
$scope.$apply(function() {
$scope.imageUrl = url;
database.on('value', function(snapshot) {
var displayProfilePic = snapshot.val().photoURL;
$scope.displayProfilePic = displayProfilePic;
});
});
});
var database = firebase.database().ref('/accounts/' + userId);
database.on('value', function(snapshot) {
var displayName = snapshot.val().name;
var description = snapshot.val().description;
var displayHobbies = snapshot.val().hobbies;
var displayFacebook = snapshot.val().facebook;
var displayTwitter = snapshot.val().twitter;
var displayInstagram = snapshot.val().instagram;
var displayYoutube = snapshot.val().youtube;
var displaySnapchat = snapshot.val().snapchat;
var displayLinkedin = snapshot.val().linkedin;
var displayProfilePic = snapshot.val().photoURL;
$scope.$apply(function() {
$scope.displayName = displayName;
$scope.description = description;
$scope.displayHobbies = displayHobbies;
$scope.displayFacebook = displayFacebook;
$scope.displayTwitter = displayTwitter;
$scope.displayInstagram = displayInstagram;
$scope.displayYoutube = displayYoutube;
$scope.displaySnapchat = displaySnapchat;
$scope.displayLinkedin = displayLinkedin;
$scope.displayProfilePic = displayProfilePic;
// Hide Social buttons if no value in Database :
if (displayFacebook === "") {
document.getElementById('iconFacebook').style.color = '#EFF1F5';
var anchor = document.getElementById("linkFacebook"); // Get the <a> element with id="myAnchor"
var att = document.createAttribute("href"); // Create a "href" attribute
att.value = "#"; // Set the value of the href attribute
anchor.setAttributeNode(att);
} else {
document.getElementById('iconFacebook').style.color = '#80d5f2';
var anchor = document.getElementById("linkFacebook"); // Get the <a> element with id="myAnchor"
var att = document.createAttribute("href"); // Create a "href" attribute
att.value = "http://www.facebook.com/" + displayFacebook; // Set the value of the href attribute
anchor.setAttributeNode(att); // Add the href attribute to <a>
};
if (displayTwitter === "") {
document.getElementById('iconTwitter').style.color = '#EFF1F5';
var anchor = document.getElementById("linkTwitter"); // Get the <a> element with id="myAnchor"
var att = document.createAttribute("href"); // Create a "href" attribute
att.value = "#"; // Set the value of the href attribute
anchor.setAttributeNode(att);
} else {
document.getElementById('iconTwitter').style.color = '#80d5f2';
var anchor = document.getElementById("linkTwitter"); // Get the <a> element with id="myAnchor"
var att = document.createAttribute("href"); // Create a "href" attribute
att.value = "http://www.twitter.com/" + displayTwitter; // Set the value of the href attribute
anchor.setAttributeNode(att);
};
if (displayInstagram === "") {
document.getElementById('iconInstagram').style.color = '#EFF1F5';
var anchor = document.getElementById("linkInstagram"); // Get the <a> element with id="myAnchor"
var att = document.createAttribute("href"); // Create a "href" attribute
att.value = "#"; // Set the value of the href attribute
anchor.setAttributeNode(att);
} else {
document.getElementById('iconInstagram').style.color = '#80d5f2';
var anchor = document.getElementById("linkInstagram"); // Get the <a> element with id="myAnchor"
var att = document.createAttribute("href"); // Create a "href" attribute
att.value = "http://www.instagram.com/" + displayInstagram; // Set the value of the href attribute
anchor.setAttributeNode(att);
};
if (displayYoutube === "") {
document.getElementById('iconYoutube').style.color = '#EFF1F5';
var anchor = document.getElementById("linkYoutube"); // Get the <a> element with id="myAnchor"
var att = document.createAttribute("href"); // Create a "href" attribute
att.value = "#"; // Set the value of the href attribute
anchor.setAttributeNode(att);
} else {
document.getElementById('iconYoutube').style.color = '#80d5f2';
var anchor = document.getElementById("linkYoutube"); // Get the <a> element with id="myAnchor"
var att = document.createAttribute("href"); // Create a "href" attribute
att.value = "http://www.youtube.com/" + displayYoutube; // Set the value of the href attribute
anchor.setAttributeNode(att);
};
if (displaySnapchat === "") {
document.getElementById('iconSnapchat').style.color = '#EFF1F5';
var anchor = document.getElementById("linkSnapchat"); // Get the <a> element with id="myAnchor"
var att = document.createAttribute("href"); // Create a "href" attribute
att.value = "#"; // Set the value of the href attribute
anchor.setAttributeNode(att);
} else {
document.getElementById('iconSnapchat').style.color = '#80d5f2';
var anchor = document.getElementById("linkSnapchat"); // Get the <a> element with id="myAnchor"
var att = document.createAttribute("href"); // Create a "href" attribute
att.value = "http://www.snapchat.com/" + displaySnapchat; // Set the value of the href attribute
anchor.setAttributeNode(att);
};
if (displayLinkedin === "") {
document.getElementById('iconLinkedin').style.color = '#EFF1F5';
var anchor = document.getElementById("linkLinkedin"); // Get the <a> element with id="myAnchor"
var att = document.createAttribute("href"); // Create a "href" attribute
att.value = "#"; // Set the value of the href attribute
anchor.setAttributeNode(att);
} else {
document.getElementById('iconLinkedin').style.color = '#80d5f2';
var anchor = document.getElementById("linkLinkedin"); // Get the <a> element with id="myAnchor"
var att = document.createAttribute("href"); // Create a "href" attribute
att.value = "http://www.linkedin.com/" + displayLinkedin; // Set the value of the href attribute
anchor.setAttributeNode(att);
};
});
});
}])
感谢您的建议!
编辑:我将我的应用程序与我认为不会停止屏幕每次转换之间的循环的标签一起使用?也许?
【问题讨论】:
-
当
pathReference.getDownloadURL得到解决时,你为什么要打电话给$scope.$apply? -
亲爱的上帝,把你的代码包装在一个函数中!
-
如果摘要已经在进行中,显然不需要以 $scope.$apply 开头。
-
大家好,我对 Javascript 和 Ionic 还很陌生,所以我尝试使用在线教程等...我上周在 Codecademy 上学到了我能学到的东西,所以是的,我仍然有很多问题。如果您能向我展示一种更好的方法来组织我的代码和函数,我将不胜感激,这样我就不必使用 Scope one。我根本不知道任何其他方式!
标签: javascript angularjs firebase ionic-framework firebase-realtime-database