2016-09-16 2 views
0

5秒後にスコープの値を更新する必要があるという問題に直面しています。ここで私の値を初期化し、5秒後にコンテンツをリフレッシュするタイムアウト関数を呼び出す私のコントローラのコンストラクタ関数です。コンストラクタのtypescriptで5秒後にスコープの値を更新する方法

static $inject = ['$scope', 'selectedCardAccount', 'CardService', 'ecAnimationService', '$interval', '$modal','$rootScope','$timeout','PromiseTimeout','$q','$stateParams']; 
      constructor(public $scope,public selectedCardAccount, public CardService:services.CardService, public $interval:ng.IIntervalService, public $modal,public $rootScope:ng.IRootScopeService, public $timeout,public PromiseTimeout,public $q,public TrackPendingRequest,public $stateParams) { 

    //setting the current value 
    this.account = selectedCardAccount.account; 

    //calling a serive again after five section to referesh the content 
    $timeout(function(){ 
      //delete the current object 
     delete this.account; 
     //calling service to get result 
       CardService.getAccountAndCardByShortName($stateParams.productShortName,$stateParams.id).then((succRess)=>{ 

      //setting the value 
      //Error : cannot read property of account 
      this.account = succRess.account; 
     }); 
    }, 5000); 
} 

エラー:アカウントのプロパティを読み取ることができません

指令コード:

module ec.directives { 
     export class easein{ 
      /** 
      * A directive is not new:ed up so we don´t specify anything on this class 
      * */ 
      constructor(){ 
       var directive: ng.IDirective = {}; 
       directive.link = (scope:IEaseInNumberScope, element, attrs, ctrl:controllers.EaseInNumberCtrl) => { 
        ctrl.animateRemainingValue(scope); 
       }; 
       directive.restrict = "EA"; 
       directive.template = "{{current | number : 2}}"; 
       directive.controller = 'EaseInNumberCtrl'; 
       directive.scope = { 
        duration: "=?", 
        startValue: "=?", 
        endValue: "=" 
       }; 

       return directive; 
      } 
     } 
} 

HTML:

<div class="row"> 
    <div class="col-xs-6"> 
     <h2 class="heading-small-bold light mar-l-1" 
      ng-bind="'used_amount' | i18n"></h2> 

     <h1 class="heading-big-thin-medium mar-l-1"> 
      <easein end-value="vm.account.usedCredit"></easein> 
     </h1> 
    </div> 
    <div class="col-xs-6 align-r"> 
     <h2 class="heading-small-bold light mar-r-1" 
      ng-bind="'left_to_spend' | i18n"></h2> 

     <h1 class="heading-big-thin-medium mar-r-1"> 
      <easein start-value="vm.account.creditLimit" 
        end-value="vm.account.openToBuy"></easein> 
     </h1> 
    </div> 
    <div class="col-xs-12 mar-t-2"> 
     <progressbar class="progress-bar" value="vm.loadingValue" 
        max="vm.account.creditLimit" 
        type="warning"></progressbar> 
     <div class="flexible-header"> 
      <h2 class="heading-small-bold light" 
       ng-bind="'last_transactions' | i18n"></h2> 
     </div> 
    </div> 
</div> 

コンストラクタの5つのセクションの後にどのように値をリフレッシュすることができるかという提案があれば、助けてください。

+0

は本当に間違っていますか?通常は 'あなたは何のプロパティpropNameも読むことができません。 'を得る。このエラーが発生する箇所に行番号を付けることができますか? – iberbeu

答えて

0

あなたのアプローチに間違いがあると思います。

まず、$ timeoutコールバックでコンストラクタのthisコンテキストが正しく設定されるように、Typescriptでラムダ(矢印関数)を使用する必要があります。

第2に、deleteキーワードを使用する理由はありません。初期オブジェクト参照を一定に保ちながら、this.accountの値を新しい結果で設定します。エラーメッセージ1として

static $inject = ['$scope', 'selectedCardAccount', 'CardService', 'ecAnimationService', '$interval', '$modal','$rootScope','$timeout','PromiseTimeout','$q','$stateParams']; 
 
constructor(public $scope,public selectedCardAccount, public CardService:services.CardService, public $interval:ng.IIntervalService, public $modal,public $rootScope:ng.IRootScopeService, public $timeout,public PromiseTimeout,public $q,public TrackPendingRequest,public $stateParams) { 
 

 
    //setting the current value 
 
    this.account = selectedCardAccount.account; 
 

 
    //calling a serive again after five section to referesh the content 
 
    $timeout(() => { 
 

 
     CardService 
 
      .getAccountAndCardByShortName($stateParams.productShortName,$stateParams.id) 
 
      .then((succRess)=>{ 
 
        angular.copy(succRess.account, this.account); 
 
       }); 
 
    }, 5000); 
 
}

+0

今、 'this.account'は更新されますが、ビューの値は変更されません。私はこの値を表示するためにカスタムディレクティブを持っています – Toretto

+0

これは、初期オブジェクトへの参照が変更されたためです。 'this.account'のタイプは何ですか?最初の参照を保持し、重要なプロパティを更新します。 – VRPF

+0

カスタムディレクティブ – Toretto

0

、アカウントがsuccRessでは使用できません。

succRessが有効なJSONオブジェクトであるかどうかを確認してください。サービスがサーバーからのデータをそのまま返す場合は、JSON.parseを使用してJSONオブジェクトに逆シリアル化する必要があります。

また、値の変更はそのプロパティのリスナーによって認識されるように、$ timeoutの使用を検討するとよいでしょう。

+0

で更新しました。succRessが適切なオブジェクトを返しています。私はちょうどthis.accountに割り当てる必要があります – Toretto

+0

succRessオブジェクトのアカウントプロパティが表示されますか? – Sreekanth

+0

はい、それはその値を持っています – Toretto

関連する問題