简体   繁体   English

角度2:Ts:嵌套函数

[英]Angular 2: Ts: Nested functions

I'm working on a function and can someone give advice on how to specify a function that is out side a function? 我正在研究一个功能,有人可以就如何指定功能之外的功能提供建议吗? Inside the if statment I would like to call the otherfunction(). 在if语句中,我想调用otherfunction()。

@Injectable()
export class menuService {
    constructor (){}
    testing(){ console.log('something')}
    loadwidget(){


           // not able to call this function
           this.testing()

    }
}

Error that i get is "this.testing is not a function" 我得到的错误是“ this.testing不是函数”

ERROR TypeError: this.testing is not a function
    at Object.menuService.loadwidget (http://localhost:3000/main.bundle.js:755:14)
    at Object.eval [as handleEvent] (ng:///AppModule/tbuttonsComponent.ngfactory.js:36:41)
    at handleEvent (http://localhost:3000/vendor.dll.js:13146:138)
    at callWithDebugContext (http://localhost:3000/vendor.dll.js:14354:42)
    at Object.debugHandleEvent [as handleEvent] (http://localhost:3000/vendor.dll.js:13942:12)
    at dispatchEvent (http://localhost:3000/vendor.dll.js:10121:21)
    at http://localhost:3000/vendor.dll.js:10711:38

https://plnkr.co/edit/XCHsu19UhR9wWxz4VLOx?p=preview https://plnkr.co/edit/XCHsu19UhR9wWxz4VLOx?p=preview

That's exactly how you're supposed to access other methods within a class, off of the this keyword. 正是这种方式,您应该使用this关键字访问类中的其他方法。 If you're using your class above exactly as it's written out, then the issue is that variableName isn't defined anywhere within customfunction() so it errors out. 如果您在上面完全按照编写的方式使用类,则问题在于在customfunction()任何地方都customfunction() variableName因此会出错。 The console.log() statement in otherfunction() never gets a chance to run because of that. 因此, otherfunction()console.log()语句永远不会有运行的机会。

Edit: I took a look at the Plunker you added in, it turns out it's a scoping issue. 编辑:我看了一下您添加的Plunker,事实证明这是一个范围问题。 I updated the menuService class, using arrow functions to implicitly bind this to menuService , and the third button started working as expected: 我更新了menuService类,则使用箭头功能隐式绑定thismenuService ,第三个按钮开始按预期工作:

export class menuService {
  constructor (){
    // I moved this into the constructor and updated loadingwidget below
    this.menu = [{
      id: 1,
      loadingwidget: () => { this.loadwidget(); },
    }];
  }

  testing(){ 
    console.log('something');
    alert('something');
  }

  loadwidget(){
    this.testing();
  }
}

Here's a working version of your Plunker: https://plnkr.co/edit/sF08cccRb2b0xkfTspVV?p=preview 这是您的Plunker的工作版本: https ://plnkr.co/edit/sF08cccRb2b0xkfTspVV ? p = preview

How you have defined should work. 您如何定义应该起作用。 If otherfunction() isn't called check you if statement, maybe the comparison doesn't return what you expect. 如果未调用otherfunction()来检查if语句,则比较可能不会返回您期望的结果。

Please look at this plunker example: https://plnkr.co/edit/4MWYDdeS5cCTDHI64HO2?p=preview 请查看以下示例: https ://plnkr.co/edit/4MWYDdeS5cCTDHI64HO2 ? p = preview

This is not because of Injectable and you don't need Injectable also as you are not injecting anything into the service. 这不是因为Injectable所致,并且因为您没有向服务中注入任何东西,所以您也不需要Injectable。 So you can omit that. 因此,您可以忽略它。 But I could see there are few issue in the code like this one, 但是我可以看到这样的代码几乎没有问题,

 menu = [
      {
        id = 1,
        loadingwidget: this.loadwidget
      }
    ]

where it should be 应该在哪里

menu = [
      {
        id:1,
        loadingwidget: this.loadwidget
      }
    ]

and it was not compiled properly. 而且没有正确编译。 It works just fine. 它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM