のコンストラクタよりもどこでもエールは基本的に私はこのコードを持っているの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も試しましたが、ダイジェストが既に進行中であると言うエラーが表示されます。しかし、私がページ上で何らかの手動操作(例えばクリック)を行うと、ページの通貨は自動的に私が約束の中で設定した新しい値に更新されます。
なぜあなたのIDのようにコンストラクタに '$ locale'を格納しないのですか? 'this。$ locale = $ locale;' –