Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.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 $log匿名函数不工作_Javascript_Angularjs_Anonymous Function_Webtorrent - Fatal编程技术网

Javascript $log匿名函数不工作

Javascript $log匿名函数不工作,javascript,angularjs,anonymous-function,webtorrent,Javascript,Angularjs,Anonymous Function,Webtorrent,我尝试在webtorrent函数中记录一些数据时遇到问题 我想记录this.client.add的一些值,但我没有访问权限 知道这里发生了什么吗 import Webtorrent from 'webtorrent'; class PlaylistController { /** @ngInject */ constructor($http, $log) { this.log = $log; this.client = n

我尝试在webtorrent函数中记录一些数据时遇到问题

我想记录this.client.add的一些值,但我没有访问权限

知道这里发生了什么吗

    import Webtorrent from 'webtorrent';

    class PlaylistController {
      /** @ngInject */
      constructor($http, $log) {
        this.log = $log;
        this.client = new Webtorrent();

        $http
          .get('app/playlist/playlist.json')
          .then(response => {
            this.Torrent = response.data;
          });
      }

      addTorrent(magnetUri) {
        this.log.log(magnetUri);

        this.client.add(magnetUri, function (torrent) {
          // Got torrent metadata!
          this.log.log('Client is downloading:', torrent.infoHash);

          torrent.files.forEach(file => {
            this.log(file);
          });
        });
        this.log.log('sda');
        this.log.log(this.client);
      }
    }

    export const playlist = {
      templateUrl: "app/playlist/playlist.html",
      controller: PlaylistController,
      bindings: {
        playlist: '<'
      }
    };
从“Webtorrent”导入Webtorrent;
类播放控制器{
/**@ngInject*/
构造函数($http,$log){
this.log=$log;
this.client=new Webtorrent();
$http
.get('app/playlist/playlist.json')
。然后(响应=>{
this.Torrent=response.data;
});
}
addTorrent(magnetUri){
this.log.log(magnetUri);
this.client.add(magnetUri,函数(torrent){
//获得了torrent元数据!
this.log.log('客户端正在下载:',torrent.infoHash);
torrent.files.forEach(文件=>{
此.log(文件);
});
});
this.log.log('sda');
this.log.log(this.client);
}
}
导出常量播放列表={
templateUrl:“app/playlist/playlist.html”,
控制器:播放控制器,
绑定:{

playlist:“您需要将此(类)作为另一个变量引用,以便在函数(torrent)函数中使用,或者使用箭头函数,以便此引用保持为类引用

解决方案1,使用另一个变量引用类:

addTorrent(magnetUri) {
    this.log.log(magnetUri);

    var that = this;

    this.client.add(magnetUri, function (torrent) {
      // Got torrent metadata!
      that.log.log('Client is downloading:', torrent.infoHash);

      torrent.files.forEach(file => {
        that.log(file);
      });
    });
    this.log.log('sda');
    this.log.log(this.client);
  }
解决方案2,使用箭头函数:

addTorrent(magnetUri) {
    this.log.log(magnetUri);

    this.client.add(magnetUri, torrent => {
      // Got torrent metadata!
      this.log.log('Client is downloading:', torrent.infoHash);

      torrent.files.forEach(file => {
        this.log(file);
      });
    });
    this.log.log('sda');
    this.log.log(this.client);
  }

在JS文件的第一行添加
/*eslint no console:[“error”、{allow:[“warn”,“error”]}]*/
以删除
console.log()
警告。是的,这是另一种解决方案,但我想了解为什么我不能在函数中使用$log。您正在使用
这个
内部函数(torrent){…}在纯ES6中,您可以使用arrow函数而不是普通函数,以便此引用仍然是外部引用。