0

Есть котроллер в котором стороняя библиотека (да на джавастрипте)))) "dhtmlXTreeObject" который рисует дерево. у него есть свой КликХандлер. И я в функции клика присваиваю значение глобальному переменному. но проблема в том что переменная не обновляется. Помогите пожалуйста Переменные type1 type2. Функции tonclick_checks и tonclick.

Я понимаю что js библиотеки подключать не хорошо и tonclick_checks и tonclick срабатывает не в области видимости контроллера. Но как бы обновить переменные. Спасибо большое заранее.

Import {
  AfterViewInit, Component, DoCheck, EventEmitter, NgModule, NgZone, OnDestroy, OnInit,
  Output
} from '@angular/core';

declare var dhtmlXTreeObject: any;


@Component({
  selector: 'ngx-treemo',
  styleUrls: ['./treemo.component.scss'],
  templateUrl: './treemo.component.html',
})
export class TreemoComponent implements AfterViewInit, OnDestroy, OnInit, DoCheck {
  type: ChartType;
  graphic: boolean;
  table: boolean;
  chartTypes: ChartType[];
  data: any;
  modael1: any;
  model: any;
  modaltree: any;
  type1: string;
  type2: string;
  newtype1: string;
  newtype2: string;
  options: any = {};
  themeSubscription: any;

  constructor(private http: HttpClient,
              private theme: NbThemeService,
              private zone: NgZone ) {
    this.graphic = true;
    this.table = false;
    this.chartTypes = ['Bar', 'Line', 'Radar'];
    this.type = 'Bar';
    this.type1 = '523';
    this.type2 = '1t1';
    this.newtype1 = '523';
    this.newtype2 = '0t';

  }


  onChange(deviceValue) {
  }

  ngOnInit(): void {

    const myTree_checks = new dhtmlXTreeObject('treeboxbox_tree_checks', '100%', '100%', 0);
    myTree_checks.setImagePath('../../../../assets/skinstree/web/imgs/dhxtree_web/');
    myTree_checks.enableDragAndDrop(true);
    myTree_checks.setOnClickHandler(this.tonclick_checks);
    myTree_checks.enableMultiLineItems('100%');


    const myTree1 = new dhtmlXTreeObject('treeboxbox_tree_types', '100%', '100%', 0);
    myTree1.setImagePath('../../../../assets/skinstree/web/imgs/dhxtree_web/');
    myTree1.enableDragAndDrop(true);
    myTree1.setOnClickHandler(this.tonclick);
    myTree1.enableMultiLineItems('100%');

    }


  tonclick_checks(id)   {
    this.type1 = id;
   // console.log(this.type1);
  }
  tonclick(id) {
    this.type2 = id;
   // console.log(this.type2);
  }
}
1
  • Ну так что, работает? 26 апр 2018 в 18:31

2 ответа 2

0

Для того, что бы агуляр увидел изменения можно запустить специальную функцию ngZone.run.

Пример:

 tonclick(id) {
    this.zone.run(()=>{
      this.type2 = id;
    });
  }

Еще не помешало бы прокинуть this в обработчик, можно попробовать вот так.

myTree1.setOnClickHandler((id)=>this.tonclick(id));

Если не получается, можно еще вот так попробовать.

myTree1.setOnClickHandler(this.tonclick.bind(this));
3
  • Cannot read property 'run' of undefined
    – Serik
    25 апр 2018 в 9:03
  • Не могу обращатся к this(component) внутри функции
    – Serik
    25 апр 2018 в 9:24
  • @SerikStartAndroid А вы изменили setOnClickHandler? Если да, покажите в вопросе ваш акутальный код. 25 апр 2018 в 11:24
0
myTree1.setOnClickHandler((id) => {
      this.zone.run(() => {
        this.type1 = id;
      });
      this.highchartsbuild();
    });

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.