Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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
Angular DomsanizationService未定义_Angular - Fatal编程技术网

Angular DomsanizationService未定义

Angular DomsanizationService未定义,angular,Angular,在我的组件中,我尝试为属性执行双向绑定。以下是我的组件代码: constructor(public _DomSanitizationService: DomSanitizer ) {} ngOnInit(){ this.hello(); this.intervalId = setInterval(this.hello, 1000); } hello(){ this.socket.on('image_data', (data: any) => {

在我的组件中,我尝试为属性执行双向绑定。以下是我的组件代码:

  constructor(public _DomSanitizationService: DomSanitizer ) {}

  ngOnInit(){
    this.hello();
    this.intervalId = setInterval(this.hello, 1000);
  }

  hello(){
    this.socket.on('image_data', (data: any) => {
      this.image = this._DomSanitizationService.bypassSecurityTrustUrl("data:image/png;base64, " + data;
    });
  }

ngOnInit执行时没有错误,但当
setInterval
执行
此操作时没有错误。_domsanizationservice
未定义。我不知道是什么原因造成的。

您必须重新分配this的值,以便在函数中获得作用域。尝试以下解决方案:

  hello(){
    var _this = this;
    this.socket.on('image_data', (data: any) => {
      _this.image = _this._DomSanitizationService.bypassSecurityTrustUrl("data:image/png;base64, " + data;
    });
  }