2016-10-28 15 views
1

エラーメッセージをビューに表示しようとしました。コントローラからのサービスコールが404になると、パスはログインページビューにリダイレクトされ、エラー404が発生したときに表示する$ scopeにメッセージが設定されています。以下ビューにエラーメッセージを表示する必要があります。コントローラにエラーメッセージが設定されています。

は)] 404は

angular.module('app.controllers').controller('contactLoginCtrl',['$rootScope','$scope','AuthenticateService','$location',function($rootScope,$scope,AuthenticateService,$location){ 


$scope.authenticate = function(userModel){ 
    var userName = userModel.username; 
    var password = userModel.password; 
    console.log(userName+password); 
    var result = AuthenticateService.checkUser(userName,password); 

    result.then(function(response){ 
     console.log(response); 

    },function(error){ 

     if(error.status== '404'){ 

      // ***below is the erroMssg which has to show on the view, 
       after setting the message it being redirected to login view*** 

      $scope.errorMsg = "your request not been processed"; 
      $location.path('/'); 
     } 
    }) 

}; 

}を発生したときに$スコープにエラーメッセージを設定する制御コードです。私はこれが動作していない{{}} errorMsg内容

としてerrorMsg内容の表現を配置していますビューで

は、誰もが私にどんなトレーニングを提案してくださいだろうか?事前に感謝します

+0

を有するであろうHTMLコード – Sajeetharan

+0

を示し{{errorMsg内容}} <フォーム名= "contactLogin" NGコントローラ= "contactLoginCtrl"> \t \t \t \t \t \t <= "パスワード" のラベル> /> \t \t \t
\t \t \t
<ボタンタイプ=名前を "送信" = "ログイン" NG-C > "(userModel)を認証" =なめるログイン \t \t \t \t \t \t \t \t \t \t \t – user1245524

答えて

1

おそらく、それはスコープダイジェストサイクルの問題です。ここでは、シナリオの単純な作業コピーを示します。データはサービスからフェッチされます。 ajaxエラーコールバックでerrMsgが設定されたら、$ scope.digest()を使って問題を解決できます。

var app = angular.module("sampleApp", []); 
 

 
app.controller("sampleController", ["$scope", "sampleService", 
 
    function($scope, sampleService) { 
 

 
    sampleService.sampleMethod(1).then(function() { 
 
     console.log("Success"); 
 
    }, function(error) { 
 
     if (error.status == '400') { 
 
     $scope.errMsg = "Unauthorized User. Please login to continue"; 
 
     } 
 
     $scope.$digest(); 
 
    }); 
 
    } 
 
]); 
 

 
app.service("sampleService", function() { 
 
    this.sampleMethod = function(value) { 
 
    var promise = new Promise(function(resolve, reject) { 
 
     setTimeout(function() { 
 
     value = value * 2; 
 
     reject({ 
 
      status: 400, 
 
      message: "UnAuthorized" 
 
     }); 
 
     }, 1000); 
 
    }); 
 
    return promise; 
 
    }; 
 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script> 
 
<div ng-app="sampleApp"> 
 
    <div ng-controller="sampleController"> 
 
    {{errMsg}} 
 
    </div> 
 
</div>

0

エラーコールバックは別scope.So

angular.module('app.controllers').controller('contactLoginCtrl',['$rootScope','$scope','AuthenticateService','$location',function($rootScope,$scope,AuthenticateService,$location){ 

$scope.errorMsg = ""; 
$scope.authenticate = function(userModel){ 
    var userName = userModel.username; 
    var password = userModel.password; 
    console.log(userName+password); 
    var result = AuthenticateService.checkUser(userName,password); 

    result.then(function(response){ 
     console.log(response); 

    },function(error){ 

     if(error.status== '404'){ 

      // ***below is the erroMssg which has to show on the view, 
       after setting the message it being redirected to login view*** 

      $scope.errorMsg = "your request not been processed"; 
      $location.path('/'); 
     } 
    }) 

}; 
関連する問題