简体   繁体   English

从组件更新Angular 5指令

[英]Angular 5 Updating Directive from Component

I'm trying to do translation for a button so when I select language from a drop down I want to change the button text using a directive. 我正在尝试翻译按钮,因此当我从下拉菜单中选择语言时,我想使用指令更改按钮文本。 Below are the respective codes. 以下是各自的代码。 home.component.ts home.component.ts

this._i18nDirective.i18nLanguage contains the Object Key which I will pick from the language JSON. this._i18nDirective.i18nLanguage包含我将从JSON语言中选择的对象密钥。

export class HomeComponent implements OnInit {

    appSelectLanguages: any = [];
    selectedAppLangaugeValue = '';
    @ViewChild(I18nDirective) _i18nDirective;
    constructor() { }

    ngOnInit = () => {
    }

    appLanguageChange__Event = () => {
        this._i18nDirective.setterMultiParams(this._i18nDirective._el.nativeElement, this._i18nDirective.i18nLanguage);
    }
}`

i18ndirectve: i18ndirectve:

`import { Directive, ElementRef, HostListener, Input, Renderer, ViewContainerRef } from '@angular/core';
import { NGXLogger } from 'ngx-logger';
import * as _ from 'underscore';
import { ConfigService } from '../providers/config.service';
import { HttpService } from '../providers/http.service';

@Directive({
  selector: '[appI18n]'
})
export class I18nDirective {

    private i18nLanguage: any = {};

    constructor(
        private _ConfigService: ConfigService,
        private _Logger: NGXLogger,
        private _el: ElementRef,
        private _Renderer: Renderer,
        private _HttpService: HttpService
    ) {
        this._ConfigService.appLanguageEvent.subscribe(value => {this.i18nLanguage = value; this._Logger.debug(value); });
     }

    @Input() set appI18n (_key: string) {
        if (this.i18nLanguage && this.i18nLanguage.data) {
            this.setter(_key);
        }
        else {
            this._HttpService.languageGetter(this._ConfigService.defaultLanguage).subscribe(response => {
                const appLangData: any = {};
                appLangData.data = response;
                appLangData.action = 'SET_APP_LANGUAGE';
                this._ConfigService.setter(appLangData);
                setTimeout(() => {
                    this.setter(_key);
                }, 1000);
            });
        }
    }

    setter = (_key) => {
        if (this.i18nLanguage.hasOwnProperty(_key)) {
            this._Renderer.setElementProperty(this._el.nativeElement, 'innerHTML', this.i18nLanguage[_key]);
        }
    }

    setterMultiParams = (_element, _key) => {
        const _keyName = Object.keys(_key);
        if (this.i18nLanguage.hasOwnProperty(_keyName)) {
            this._Renderer.setElementProperty(_element, 'innerHTML', this.i18nLanguage[_keyName[0]]);
        }
    }

}`

So the setterMultiParams method is called to update the directive. 因此,将setterMultiParams方法来更新指令。 The problem is: When I select Spanish it still shows me the English version but when I again choose English from the dropdown I get the Spanish version which I selected previously. 问题是:当我选择西班牙语时,它仍然显示英语版本,但是当我再次从下拉列表中选择英语时,我得到了之前选择的西班牙语版本。 Its not updating immediately. 它不会立即更新。

This is the button: 这是按钮:

<button mat-raised-button color="primary" class="_rx_fat_button _rx_footer_single_btn" appButtonAction [appButtonActionLabel]="'GET_STARTED'"><span [appI18n]="'GET_STARTED'"></span></button>

在此处输入图片说明

The problem was that my language object in ConfigService was not updated, this was causing to load the load json. 问题是我的ConfigService中的语言对象未更新,这导致加载load json。 This was my mistake 这是我的错

Add a delay for 250ms before calling 通话前增加250ms的延迟

this._i18nDirective.setterMultiParams(this._i18nDirective._el.nativeElement, this._i18nDirective.i18nLanguage);

and it was working as expected 它按预期工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM