[英]Angular JS Ng-Show / Ng-Hide
我正在為登錄表單編寫角度服務。 我已經編寫了一家工廠(並使用了http注入器),負責將摘要HTTP憑據提交給其余服務。 我的身份驗證正常,但是我遇到的問題與錯誤消息有關,如果提供了錯誤的憑據,我將顯示該錯誤消息。
我包括了一個帶有errorAlertTextElem的ng-show值的div。 當資源工廠的$ resolved狀態為false時,我想顯示ng-show errorAlertTextElem元素。 目前,此方法有效,但僅需片刻。 當我輸入不正確的憑據並單擊登錄時,錯誤消息將顯示一秒鍾,然后消失。 我一直試圖找出原因。 我想知道是否需要使用$ scope。$ apply(),但是由於摘要已經在進行中,因此無法正常工作。 我在這里想念什么?
角度控制器代碼:
//angular.js controller code portion
loriaGameControllers.controller('signinController', ['$scope', 'Users',function($scope,Users){
$scope.signin = function()
{
//Users is a factory that makes a restful resource call.
var test = Users.userInfo().query();//this returns a promise and $resolved
console.log(test);
if(test.$resolved == false){
$scope.errorAlertTextElem = true;//right here is where I attempt to set the ng-show value to true so that the error box will be true. However, the errorAlertTextElem only displays for a second and then goes away.
}
};
}]);
html代碼登錄表單代碼(位於我的ng-app中):
<form class="form-signin col-md-6" role="form" name="signinForm" ng-controller="signinController">
<h2 class="form-signin-heading">Sign In</h2>
<!-- The error message I want to show-->
<div class="alert alert-danger" ng-show="errorAlertTextElem">Invalid Username or Password</div>
<input type="email" class="form-control" placeholder="Email address" ng-model="username" name="username"required autofocus>
<input type="password" class="form-control" placeholder="Password" ng-model="password" name="password"required>
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>
<!--ng-click that triggers the callback-->
<div class="btn btn-lg btn-primary btn-block" ng-click="signin()">Sign in</div>
</form>
只有在發生http錯誤時,角度資源才會拒絕。 這意味着,當發生類似“ 404”或“ 505”的錯誤時,解決方案將為假。 要使代碼正常工作,您應該處理來自服務器的resolve響應以顯示消息。
test.$promise.resolve(function(response){
// handle the error come from you server
console.log(response);
// show error msg?
});
我已經解決了這個問題。 我使用的進樣器位置發生了變化。 因此,每次進樣器運行時,都會重新加載模板。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.