2017-05-23 39 views
0

请参阅编辑3. 我目前正在使用Angular和Firebase构建应用程序。但我有一个问题。当我在控制器中更改范围值时,更改不会影响html。当范围值发生变化时html的延迟更改

我想使用ng-if在错误发生时显示错误警报。 我设置了<div ng-if="isThereAnyError">{{errorMessage}}</div> 而在我的控制器出现错误时,我会将范围值isThereAnyError设置为trueerrorMessage ="some error hint"。 这些更改不会在html端生效,但它会在console.log中显示错误时显示更改。

下面是完整的HTML和控制器代码

<h3 class="with-line">Ubah Password</h3> 

<div class="row"> 
    <div class="col-md-6"> 
    <div class="form-group row clearfix"> 
     <label for="inputPassword" class="col-sm-3 control-label">Password</label> 

     <div class="col-sm-9"> 
     <input ng-model="user.password" type="password" class="form-control" id="inputPassword" placeholder="" value="12345678"> 
     </div> 
    </div> 
    </div> 
    <div class="col-md-6"> 
    <div class="form-group row clearfix"> 
     <label for="inputConfirmPassword" class="col-sm-3 control-label">Confirm Password</label> 

     <div class="col-sm-9"> 
     <input ng-model="user.repassword" type="password" class="form-control" id="inputConfirmPassword" placeholder=""> 
     </div> 
    </div> 
    </div> 
</div> 
<div class="alert alert-danger" ng-if="gagalubahpassword">{{messagegagalubahpassword}}</div> 
<button ng-click="ubahPassword(user)" type="button" class="btn btn-primary btn-with-icon save-profile"> 
    <i class="ion-android-checkmark-circle"></i>Ubah Password 
</button> 

控制器。

$scope.ubahPassword = function(user){ 

    if(typeof user!='undefined'){ 
    if(user.password !='' && user.password === user.repassword){ 

     // $scope.gagalubahpassword = false; 
     appAuth.currentUser.updatePassword(user.password).then(function() { 
     // Update successful. 
     console.log("password berhasil diubah"); 
     }, function(error) { 
     // An error happened. 
     console.log("password gagal diubah"); 
     console.log(error.message); 
     $scope.messagegagalubahpassword = "Error : "+error.message; 
     $scope.gagalubahpassword = true; 
     }); 
    }else { 
     $scope.messagegagalubahpassword = "Pastikan kolom password terisi dengan benar dan sama"; 
     $scope.gagalubahpassword = true; 
     console.log("password tidak cocok"); 
    } 
    }else{ 
    $scope.messagegagalubahpassword = "Pastikan kolom password terisi dengan benar dan sama"; 
    $scope.gagalubahpassword = true; 


    } 
} 

奇怪的是,当我编辑的文本字段,在HTML被更新,但就是它,如果我没有做任何事情(更改文本字段值),然后将HTML不会改变。

我是新角度,所以任何建议将不胜感激。

编辑: 我只是发现问题可能与我为Firebase实现代码的方式有关,因为我只注意到只有当错误来自Firebase错误回调时才会发生延迟。如果错误是因为该字段为空(在向Firebase发出请求之前进行的检查),则所有内容都只是像希望的那样工作,错误消息正在显示。 不幸的是,这是我知道如何使用Firebase的唯一方法。

编辑2: 这里是我用它来检查,如果字段匹配与否,或wheter用户输入任何输入字段或不

if(typeof user!='undefined'){ //to check if user actually input anything 
//to check if password filed and repassword field is match 
if(user.password !='' && user.password === user.repassword){ 

     // the code to change user password in firebase auth 
     appAuth.currentUser.updatePassword(user.password).then(function() { 
     // Update successful. 
     console.log("password berhasil diubah"); 
     }, function(error) { 
     // An error happened. 
     // Error that come from firebase, the problem is only happen in this block. 
     console.log("password gagal diubah"); 
     console.log(error.message); 
     $scope.messagegagalubahpassword = "Error : "+error.message; 
     $scope.gagalubahpassword = true; 
     }); 
}else { 
//change on this block is working just fine 
    $scope.messagegagalubahpassword = "Pastikan kolom password terisi dengan benar dan sama"; 
    $scope.gagalubahpassword = true; 
    console.log("password tidak cocok"); 
} 
}else{ //change on this block is working just fine 
$scope.messagegagalubahpassword = "Pastikan kolom password terisi dengan benar dan sama"; 
$scope.gagalubahpassword = true; 
} 

的行为是工作酷似我想它的代码至。我的意思是,我从控制器范围更新的消息的错误div显示没有问题。

然而,当第一和第二,如果是支票,以及应用程序开始执行这些代码行(即与火力的代码)

appAuth.currentUser.updatePassword(user.password).then(function() { 
     // Update successful. 
     console.log("password berhasil diubah"); 
     }, function(error) { 
     // An error happened. 
     console.log("password gagal diubah"); 
     console.log(error.message); 
     $scope.messagegagalubahpassword = "Error : "+error.message; 
     $scope.gagalubahpassword = true; 
     }); 

就是在这个时候我意识到这可能是问题用我实现Firebase代码的方式。

编辑3. 我能够完成这项工作,但我不知道该解决方案的解释,所以我只是把它留在这个问题而不是制造一个答案,所以如果有人来到这个页面或者有相同的问题可以在有人解释它之前将其作为临时解决方案。

在Firebase错误回调中,我添加了$scope.$apply()来强制应用程序更新html,这是工作。但是,我不知道为什么,我认为这是与摘要循环或某事有关的,

+0

它不是完全明显的是什么,从您发布的代码怎么回事,但你都落入具有角一个很常见的陷阱。 '$ scope.messagegagalubahpassword'和'$ scope.gagalubahpassword'都是原语,并且受到[范围继承问题](https://stackoverflow.com/a/14049482/2495283)的影响。仔细阅读那篇文章*。 **总是在角度绑定中使用一个点** – Claies

+0

“我只知道问题可能与我为Firebase实现代码的方式相同”然后,您可能想要显示该代码,因为没有任何firebase在上面的例子中。 –

+0

@DanielBeck这里是我用来检查字段是否匹配的代码,或者是用户向输入字段输入什么或不输入if(typeof user!='undefined')if(user.password!=' '&& user.password === user.repassword){' 行为完全像我想要的那样工作。我的意思是,我从控制器范围更新的消息的错误div显示没有问题。 – user3791830

回答

2

如果您已将您的HTML封装在ng-if, ng-switch ng-repeat..或其他一些用于创建新子作用域的指令中,那么您的控制器物业是overridden by the new child scope。见this example

所以最好的做法是always have . in your model。 将您的错误保持模型更改为层次模型以利用原型继承。

更改它像$scope.errorModel = {isThereAnyError:false, errorMessage : ''},然后用它喜欢:

<div ng-if="errorModel.isThereAnyError">{{errorModel.errorMessage}}</div> 
+0

谢谢你的回答,我尝试了你的建议,但它仍然不起作用。 我只知道问题可能出在我实现Firebase代码的方式上,因为我只注意到只有当错误来自Firebase错误回调时才会发生延迟。 – user3791830

+0

@ user3791830:我通过手工摘要'$ scope。$ apply()'看到了您的解决方案,我也无法弄清楚确切的原因,而无需查看更多的代码,但只是一个建议,如果您必须做一个手工摘要,确保现有的摘要没有通过'$$ phase'进行。 [阅读更多](https://stackoverflow.com/questions/20263118/what-is-phase-in-angularjs) – anoop

相关问题