Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/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
Angularjs 可以重写这个,这样我就不会';在使用承诺时,不必将其分配给变量_Angularjs_Typescript_Typescript2.0 - Fatal编程技术网

Angularjs 可以重写这个,这样我就不会';在使用承诺时,不必将其分配给变量

Angularjs 可以重写这个,这样我就不会';在使用承诺时,不必将其分配给变量,angularjs,typescript,typescript2.0,Angularjs,Typescript,Typescript2.0,我们在TypeScript和这个方面遇到了一些问题。是否可以重写下面的回调而不将其分配给该回调 在我的命名空间中,在与此组件相同的文件中,我有一个枚举: enum ToggleStates { State1 = 1, State2, State3 } 然后在同一组件中的函数中: let that = this; defer.then(function(response) { if (response.success !== null) { // if ther

我们在TypeScript和这个方面遇到了一些问题。是否可以重写下面的回调而不将其分配给该回调

在我的命名空间中,在与此组件相同的文件中,我有一个枚举:

enum ToggleStates {
  State1 = 1,
  State2,
  State3
}
然后在同一组件中的函数中:

  let that = this;

  defer.then(function(response) {
      if (response.success !== null) { // if there is a success handler
        that.$rootScope.$broadcast("w-signed", this.toggleStates, this.currentSigned);
      }else {
        that.$rootScope.addAlert("danger", error);
      }
  }).catch(function(response) {
      that.$rootScope.addAlert("danger", error);
  }).finally(function() {
      that.$rootScope.addAlert("danger", error);
  });
对于类似的问题,我们使用lambdas来回避这个问题。我尝试了以下方法:

  defer.then = (response) => {
      if (response.success !== null) { // if there is a success handler
        this.$rootScope.$broadcast("w-signed", this.toggleStates, this.currentSigned);
      }else {
        this.$rootScope.addAlert("danger", error);
      }
  }

  defer.catch = (response) => {
       this.$rootScope.addAlert("danger", error);
  }

  defer.finally = () => {
    this.$rootScope.addAlert("danger", error);
  }    
但是我得到了以下错误:

wp-leftnavcomponent.ts(208,65): error TS2339: Property 'toggleStates' does not exist on type 'WpListComponentController'.

您正在用lambda覆盖
,然后
/
捕获
/
最后
,而不是传递它们。在原始示例中,只需将
function(){}
替换为
()=>{}

defer.then((response) => {
    if (response.success !== null) { // if there is a success handler
        this.$rootScope.$broadcast("w-signed", this.toggleStates, this.currentSigned);
    }
    else {
        this.$rootScope.addAlert("danger", error);
    }
})
.catch((response) => {
    this.$rootScope.addAlert("danger", error);
})
.finally(() => {
    this.$rootScope.addAlert("danger", error);
});

切换状态和问题有什么关系?没有提到切换状态来自何处。很好。刚刚添加了更多信息上面的代码中没有WpListComponentController。它在类定义中遗漏了toggleStates属性。错误就是这样说的。枚举被声明为局部变量。它未附加到WpListComponentController对象。