Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/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 6-如何将服务函数指向JSON对象而不执行它_Angular_Angular6 - Fatal编程技术网

Angular 6-如何将服务函数指向JSON对象而不执行它

Angular 6-如何将服务函数指向JSON对象而不执行它,angular,angular6,Angular,Angular6,我存储了一个类似这样的JSON to_be_load: { type: string; status: number; message: string; online: { fn: Promise<any>; message: string; }; offline: { fn: Promise<any>; message: string; }; }[]; 我的目标是,如果从脱机执行函数失败,则执行联机字段中的所有函数 我加载这些数据的函数如下所示 p

我存储了一个类似这样的JSON

to_be_load: {
  type: string;
  status: number;
  message: string;
  online: { fn: Promise<any>; message: string; };
  offline: { fn: Promise<any>; message: string; };
}[];
我的目标是,如果从脱机执行函数失败,则执行联机字段中的所有函数

我加载这些数据的函数如下所示

private loadData() {
    for (const item of this.to_be_load) {
      item.online.fn.then(() => {
        item.status = 1;
        item.message = item.online.message;
        this.can_move();
      }).catch(() => {
        item.offline.fn.then(() => {
          item.status = 2;
          item.message = item.offline.message;
          this.can_move();
        }).catch(() => {
          item.status = 3;
          item.message = 'Error';
        });
      });
    }
  }

这里我的问题是函数start execute,当我将它分配给JSON时,是否有任何可能的方法以angular或typescript的方式解决这个问题,

您不是分配函数,而是分配函数的结果。应该是这样的:

this.to_be_load = [
  {
    type: 'floors',
    status: 0,
    message: 'Floors loading',
    online: {
      fn: () => this.floors.getFromCloud(),
      message: 'Floors loaded from cloud'
    },
    offline: {
      fn: () => this.floors.getFromLocal(),
      message: 'Floors loaded from local'
    }
  },
  {
    type: 'Categories',
    status: 0,
    message: 'Categories loading',
    online: {
      fn: () => this.category.getFromCloud(),
      message: 'Categories loaded from cloud'
    },
    offline: {
      fn: () => this.category.getFromLocal(),
      message: 'Categories loaded from local'
    }
  }]
然后:

private loadData() {
for (const item of this.to_be_load) {
  item.online.fn().then(() => {
    item.status = 1;
    item.message = item.online.message;
    this.can_move();
  }).catch(() => {
    item.offline.fn().then(() => {
      item.status = 2;
      item.message = item.offline.message;
      this.can_move();
    }).catch(() => {
      item.status = 3;
      item.message = 'Error';
    });
  });
}
换句话说,
fn:this.floors.getFromCloud()
这将分配
getFromCloud()函数的结果。而this.floors.getFromCloud()定义了一个函数。因此,您可以稍后在需要时使用
item.online.fn()调用它

private loadData() {
for (const item of this.to_be_load) {
  item.online.fn().then(() => {
    item.status = 1;
    item.message = item.online.message;
    this.can_move();
  }).catch(() => {
    item.offline.fn().then(() => {
      item.status = 2;
      item.message = item.offline.message;
      this.can_move();
    }).catch(() => {
      item.status = 3;
      item.message = 'Error';
    });
  });
}