Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript函数中使用/绑定角度变量_Javascript_Angular - Fatal编程技术网

如何在javascript函数中使用/绑定角度变量

如何在javascript函数中使用/绑定角度变量,javascript,angular,Javascript,Angular,我得到了一个使用javascript库(FullCalendar V4)的Angular组件。 当某些事件附加(鼠标悬停、单击等)时,库调用函数,并且有一个参数使我能够访问日历的值(开始日期、结束日期、Id等)。 我的目标是从角度变量中的javascript函数中获取数据。 我明确指出,如果在javascript函数中使用console.log,值会完美地显示出来,但我无法在组件的变量中获得这些值 我认为这是一个范围问题。角度组件无法访问javascript函数的内容,javascript函数也

我得到了一个使用javascript库(FullCalendar V4)的Angular组件。 当某些事件附加(鼠标悬停、单击等)时,库调用函数,并且有一个参数使我能够访问日历的值(开始日期、结束日期、Id等)。 我的目标是从角度变量中的javascript函数中获取数据。 我明确指出,如果在javascript函数中使用console.log,值会完美地显示出来,但我无法在组件的变量中获得这些值

我认为这是一个范围问题。角度组件无法访问javascript函数的内容,javascript函数也无法访问角度变量

下面是我的代码的一个简化示例来解释:

//general import
import { Component} from '@angular/core';

//service
import { IcsService } from '../../_Services/ics.service';

//variables
declare const FullCalendar: any;
@Component({
    selector: 'app-scheduler',
    templateUrl: './scheduler.component.html'
})
/** scheduler component*/
export class SchedulerComponent {
  ressources: any;
  event: any;
  plop: any ; //that's the var i will use for getting my javascript variable content


  /** scheduler ctor */
  constructor(private ics_service: IcsService) {}

//Add Event and ressources, that works, no worry about it
  ngOnInit() {
    this.that = this;
    this.ics_service.GetAllRessources().subscribe(result => {
      this.ressources = result;
      console.log(this.ressources);
      this.ics_service.GetAllEvents().subscribe(result => {
        this.event = result;
        console.log(this.event);
        this.calendar();       
      });
    });   
  }

  calendar() {
    var calendarEl = document.getElementById('calendar');
    var calendar = new FullCalendar.Calendar(calendarEl, {

//Parameters variables are hidden here to simplify the understanding of the code

      resources: this.ressources,
      events: this.event,
      eventClick: function (info) {

       //Here is the line that i want to achieve 
      this.plop = info.event.start.toDateString();

      console.log(info.event.start.toDateString()); //that line perfectly work

      }
    });
    calendar.render();
  }
}

感谢您的帮助。

您需要使用此处的箭头功能来访问您的
上下文:

eventClick: (info) => {
      this.plop = info.event.start.toDateString();

      console.log(info.event.start.toDateString()); //that line perfectly work
}
因此,您可以按预期使用
this.plop

更新

此外,当您使用第三方库时,视图可能不会在单击时更新。
因此,如果视图更新不起作用,请尝试在构造函数中的依赖项注入中使用
私有cd:ChangeDetectorRef
,然后在
事件中调用
this.cd.markForCheck()
,单击
回调您需要在那里使用箭头函数来访问
上下文:

eventClick: (info) => {
      this.plop = info.event.start.toDateString();

      console.log(info.event.start.toDateString()); //that line perfectly work
}
因此,您可以按预期使用
this.plop

更新

此外,当您使用第三方库时,视图可能不会在单击时更新。
因此,如果视图更新不起作用,请尝试从构造函数中的依赖项注入中使用
private cd:ChangeDetectorRef
,然后在
事件中调用
this.cd.markForCheck()
,单击
回调此
的范围在您的FullCalendar类下,您可以将角度分量的
参考存储到另一个变量中,或使用箭头函数(如Danil answer中所示)


的范围在FullCalendar类下,您可以将角度分量的
参考存储到另一个变量,或者使用箭头函数(如Danil answer中)


非常感谢,它的使用非常有效,但是箭头函数根本不起作用。非常感谢,它的使用非常有效,但是箭头函数根本不起作用。谢谢,但是我在使用箭头函数时出错了。找不到信息值。谢谢,但我在使用箭头功能时出错。找不到信息值。