2016-05-24 20 views
0

コントローラで変数が更新されていても、更新がビューに反映されていないという問題が発生しています。コントローラが更新されたときに角度ビューが更新されない

フロントエンドに表示されるはずの変数はloginErrorMessageですが、コントローラで更新されているにもかかわらず、このページに移動してから戻ると変数が表示されません。

ここ
function LoginCtrl($rootScope, $state, $scope, UserService, AttributesService){ 
    var vm = this; 
    vm.loginErrorMessage = ''; 
    vm.email = ''; 
    vm.password = ''; 
    vm.doLogin = doLogin; 
    vm.successLoginJoinApiResponse = successLoginJoinApiResponse; 

    function doLogin(){ 
     if(UserService.validateLoginCredentials()){ 
     UserService.loginUser() 
     .success(function(response){ 
      vm.successLoginJoinApiResponse(response, false); 
     }) 
     }else{ 
     vm.loginErrorMessage = 'Incorrect Email or Password'; 
     console.log(vm.loginErrorMessage) 
     //vm.loginErrorMessage is updated at this point 
     } 
    } 

    function successLoginJoinApiResponse(response, join){ 
     // stuff happens here, not where the issue is. 
    } 
    } 

がルートである:

.state('app.login', { 
    url: '/login', 
    cache: false, 
    views: { 
     'menuContent': { 
     templateUrl: 'templates/login.html', 
     controller:'LoginCtrl as app' 
     } 
    }, 
    }) 

そして、ここで図である。

<ion-view view-title="" class=""> 
    <ion-nav-bar class="white-back"> 
    </ion-nav-bar> 
    <ion-content scroll="false" class="white-back has-header"> 

    // This is where the variable should be displayed 
    <div class="login-error-message" ng-bind="app.loginErrorMessage"> 
    </div> 

    <div class="center-everything mar-top-25"> 
     <form ng-submit="app.doLogin()"> 
     <div class="list pad-20 pad-top-30 pad-bottom-0"> 
      <label class="item item-input input-login"> 
      <input type="email" placeholder="Email" ng-model="app.email"> 
      </label> 
      <label class="item item-input mar-top-15 input-login"> 
      <input type="password" placeholder="Password" ng-model="app.password"> 
      </label> 
     </div> 
     <div class="pad-20 pad-top-0"> 
      <a class="link" ng-click="app.doLogin()"> 
      <button class="button button-block blue-back white" id="button-submit-stuff" type="submit"><b>LOGIN</b></button> 
      </a> 
     </div> 
     </form> 
    </div> 
    </ion-content> 
</ion-view> 

UserService.validateLoginCredentials()がfalseを返すので、以下

はコントローラであり、スクリプトはelse文に行き、どこかで失敗します。

私は$ scope($ apply()を実行しようとしましたが、すでに進行中のエラー$ apply()を取得しています。

私は、ページから離れたところで変数が表示されることに気付きました。

また、Ionicによってこれを実行しています。

アドバイスがありましたら、本当にありがとうございます。

答えて

0

doLogin関数では、UserService.validateLoginCredentials()メソッドが非同期で約束を返していますか?その場合、elseブロック内のコードは決して実行されないためです。

+0

ありませんが、あなたも知っているだけので

function doLogin(){ if(UserService.validateLoginCredentials()){ UserService.loginUser() .success(function(response){ vm.successLoginJoinApiResponse(response, false); }) .error(function(err){ vm.loginErrorMessage = 'Incorrect Email or Password'; console.log(vm.loginErrorMessage) //vm.loginErrorMessage is updated at this point }); } else{ vm.loginErrorMessage = 'Incorrect Email or Password'; console.log(vm.loginErrorMessage) //vm.loginErrorMessage is updated at this point } } 

約束を返さない。 – RandyMarsh

0

最初にログイン資格情報を検証してログインしようとしていますか?

私は、UserService.loginUserに.error()の一部がないと推測しています。成功/エラー速記は角1.5 に廃止されていますが、この書き換えができます:実際にdoesnのサービスであるかの/ else文です

function doLogin(){ 
    if(UserService.validateLoginCredentials()){ 
     UserService.loginUser() 
      .then(function(response){ 
       vm.successLoginJoinApiResponse(response, false); 
      }, 
      function(err){ 
       vm.loginErrorMessage = 'Incorrect Email or Password'; 
       console.log(vm.loginErrorMessage); 
       //vm.loginErrorMessage is updated at this point 
      }); 
    } else{ 
     vm.loginErrorMessage = 'Incorrect Email or Password'; 
     console.log(vm.loginErrorMessage); 
     //vm.loginErrorMessage is updated at this point 
     } 
    } 
+0

ねえ、UserService.validateLoginCredentials()は実際にfalseを返しているので、スクリプトはelse文に行きます。私は最初の質問でそれを明確にすべきだった – RandyMarsh

関連する問題