gpt4 book ai didi

javascript - 在 View 更改之前, Angular 值不会反射(reflect)在范围中

转载 作者:行者123 更新时间:2023-11-30 21:02:58 25 4
gpt4 key购买 nike

下面是我的 HTML 代码:

     <div class="container-fluid">
<div class="jumbotron" id="welcomehead">

<br><br><br><br><br><br><br><br><br><br>
</div>
<div class="row">
<div class="col-md-12">
<h2>Welcome {{username }}, your Season total is {{total }}</h2>

</div>
</div>
<div class="row">
<div class="col-md-12">
<h3 id="slogan" >5 teams; 1 Goal</h3><br>
<img src="http://res.cloudinary.com/deji/image/upload/v1508887997/ymkit_ww8np1.jpg">
<img src="http://res.cloudinary.com/deji/image/upload/v1508888352/indkit_zop1gx.jpg">
<img src="http://res.cloudinary.com/deji/image/upload/v1508887290/chad2kit_fa3lrh.jpg">
<img src="http://res.cloudinary.com/deji/image/upload/v1508887718/fbg2kit_lzndap.jpg">
<img src="http://res.cloudinary.com/deji/image/upload/v1508888206/vgc_kit_hvpdz4.jpg">
</div>
</div>

下面是html代码的javascript代码:

'use strict';

angular.module('TNF.welcome', ['ngRoute', 'firebase'])

.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/welcome',{
templateUrl:'welcome/welcome.html',
controller: 'WelcomeCtrl'
});
}])

.controller('WelcomeCtrl',['$scope','$firebaseAuth','CommonProp','$location','DatabaseService',
function($scope,$firebaseAuth, CommonProp,$location, databaseService){


firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
databaseService.users.child(user.uid).once('value', function(usersSnapshot){
var users = usersSnapshot.val();
var total = users.total;
$scope.username = user.email;
$scope.total = total;

}, function(err) {
console.log(err);
$scope.total = 0;
});
} else {
// No user is signed in.
$location.path('/home');
}
});





$scope.logout = function(){
firebase.auth().signOut().then(function() {
console.log('Signed Out');
$location.path('/home');
}, function(error) {
console.error('Sign Out Error', error);
});
};

我的问题

我的问题是,作为 firebase 查询结果的 {{username}}{{total}} 值一旦页面加载。但是,一旦我离开页面然后返回 View ,这些值就会显示出来。这是否意味着 HTML 代码加载速度快于 firebase 查询可以解析?如果是这样,是否有办法让页面仅在解析 firebase 查询后加载?

最佳答案

你需要让 Angular 知道运行更新。

$scope.username = user.email; 
$scope.total = total;
$scope.$apply();

关于javascript - 在 View 更改之前, Angular 值不会反射(reflect)在范围中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46939540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com