【问题标题】:Angular values do not reflect in scope until view changes在视图更改之前,角度值不会反映在范围内
【发布时间】:2017-10-25 18:22:03
【问题描述】:

下面是我的 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}} 值不会显示在视图中。但是,一旦我离开页面然后返回视图,这些值就会显示出来。这是否意味着 HTML 代码的加载速度比 firebase 查询的解析速度更快?如果是这样,有没有办法让页面仅在 firebase 查询解决后加载?

【问题讨论】:

  • 您是否缺少将路径更改为'/welcome'?
  • @RakeshBurbure 嘿,我不小心把它放在这里了。

标签: javascript angularjs firebase web-applications firebase-realtime-database


【解决方案1】:

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

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

【讨论】:

  • 嘿@sajeetharan,如果你不介意我问,$scope.$apply(); 到底是做什么的?做?这将如何使 firebase 查询运行得更快或仅在 firebase 查询返回相关数据后才注入 html 代码?
  • 首先它与 firebase 或使查询运行更快没有任何关系。在 angularjs 中,它只是让 angular 知道值已更新。顺便说一句,它对你有用吗
  • 我认为它确实有效,因为问题不再存在。如果它在再次刷新后弹出,我会告诉你。感谢上帝保佑
  • @DejiJames 如果有帮助,请标记为答案并点赞
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 2016-01-16
  • 1970-01-01
  • 2016-06-09
相关资源
最近更新 更多