2017-11-08 3 views
0

のコンストラクタよりもどこでもエールは基本的に私はこのコードを持っているのjs $ロケール操作する:がクラス

class MainController { 
    constructor(id, $locale) { 
     this.id = id; 
     $locale.NUMBER_FORMATS.CURRENCY_SYM = '€'; //this works 
    } 

    $onInit() { 
     this._getCurrency(); 
    } 

    _getCurrency() { 
     this.getUserCurrency(this.id).then((currency) => { 
      $locale.NUMBER_FORMATS.CURRENCY_SYM = currency; // this does not work 
     }); 
    } 
} 

を私は私の$localeを変更したいが、約束が実行され、私はそれから結果を取得した後にのみ。

PS:私はまた、コンストラクタで_getCurrencyからコードを移動するために、より正確に、単にテスト目的のために何かをしようとしたが、事は$ロケールにも影響されないことである。

class MainController { 
    constructor(id, $locale) { 
     this.id = id; 
     $locale.NUMBER_FORMATS.CURRENCY_SYM = '€'; //this works 

     this.getUserCurrency(this.id).then((currency) => { 
      $locale.NUMBER_FORMATS.CURRENCY_SYM = currency; // this does not work 
     }); 
    }   
} 

getUserCurrencyは別のファイルに存在し、常にその通貨を返します。心配はありません。

編集:私はパンカジパーカー・ソリューションを使用しますが、それは動作しません、それをチェックアウト:私はthis does not workを言うとき

class MainController { 
    constructor(id, $locale) { 
     this.id = id; 
     this.$locale = $locale; 
     $locale.NUMBER_FORMATS.CURRENCY_SYM = '€'; //this works 
    } 

    $onInit() { 
     this._getCurrency(); 
    } 

    _getCurrency() { 
     this.$locale.NUMBER_FORMATS.CURRENCY_SYM = 'something'; //works but here I don't have the currency response yet 
     this.getUserCurrency(this.id).then((currency) => { 
      this.$locale.NUMBER_FORMATS.CURRENCY_SYM = currency; // this does not work 
     }); 
    } 
} 

私は$localeは、新しい値を取ることに気付きましたが、それはに更新されません。ページからは、ページからの通貨が約束の中で$ロケールが操作されたときには自動更新($filter('currency')(input);)にはなりません。

私は$ evalAsync()、$ scope。$ applyと$ scope。$ digestも試しましたが、ダイジェストが既に進行中であると言うエラーが表示されます。しかし、私がページ上で何らかの手動操作(例えばクリック)を行うと、ページの通貨は自動的に私が約束の中で設定した新しい値に更新されます。

+0

なぜあなたのIDのようにコンストラクタに '$ locale'を格納しないのですか? 'this。$ locale = $ locale;' –

答えて

0

getUserCurrency()がクラスに存在しない可能性があります。

+0

getUserCurrency()は別のファイルに存在し、値を返します。その問題はありません – paulalexandru

1

現在$localeスコープはconstructorでのみ利用可能です。したがって、コンストラクタ関数の外部では使用できません。あなたは活字体

class MainController { 
    constructor(id, $locale) { 
     this.id = id; 
     this.$locale = $locale; 
    } 

    $onInit() { 
     this._getCurrency(); 
    } 

    _getCurrency() { 
     this.getUserCurrency(this.id).then((currency) => { 
      //use _locale as $locale reference 
      this.$locale.NUMBER_FORMATS.CURRENCY_SYM = currency; 
     }); 
    } 
} 

バージョン

ES6

以下

class MainController { 
    _locale: any; 
    constructor(id, $locale) { 
     this.id = id; 
     this._locale = $locale; 
    } 

    $onInit() { 
     this._getCurrency(); 
    } 

    _getCurrency() { 
     this.getUserCurrency(this.id).then((currency) => { 
      //use _locale as $locale reference 
      this._locale.NUMBER_FORMATS.CURRENCY_SYM = currency; 
     }); 
    } 
} 

のように、その変数にごclass &割り当て$locale値に1つのパブリック変数を定義することによってこの問題を解決することができます簡略化されたo上記のアプローチは

class MainController { 
    _locale: any; 
    // use public before $locale, 
    // that would make your $locale available in your class. 
    constructor(id, public $locale) { 
     this.id = id; 
    } 

    $onInit() { 
     this._getCurrency(); 
    } 

    _getCurrency() { 
     this.getUserCurrency(this.id).then((currency) => { 
      //use _locale as $locale reference 
      this.$locale.NUMBER_FORMATS.CURRENCY_SYM = currency; 
     }); 
    } 
} 
+1

私はこれを試して答えに戻ります。もし私が間違っていないなら、私はちょっとこのようなことをやってしまいましたが、うまくいきませんでしたが、私はそれを試してみて、今朝別の場所で急ぐ必要があるので朝にお返事します。ありがとう – paulalexandru

+0

@paulalexandru np、あなたの時間を取ってください、ありがとう:) –

+0

私はそれが内部に配置されていれば私は動作しません:this.getUserCurrency(this.id).then((currency)。まだ_getCurrency()の内部にあります。問題は、そこに私が応答を持っているため、約束を内部的に必要とすることです。 – paulalexandru