我正在使用"angular-ui-tree": "^2.22.5"
单击Do something菜单项时出错。
TypeError: this.$state is undefined如何将对$state的引用传递给我的函数。
MyController:
export class MyController {
public treeMenuItems = [
{
'handleClick': this.doSomething,
'name': 'Do something'
}
];
public treeMenuPopover = {
'items': this.treeMenuItems,
'placement': 'bottom',
'title': 'Actions'
};
constructor(public $state: ng.ui.IStateService) {}
public doSomething() {
this.$state.go('my.state');
}
}
MyController.$inject = ['$state'];发布于 2017-07-19 18:47:05
你可以在Javascript here中读到this是如何工作的。
对于您的代码,请尝试
export class MyController {
public treeMenuItems = [
{
'handleClick': this.doSomething,
'name': 'Do something'
}
];
public treeMenuPopover = {
'items': this.treeMenuItems,
'placement': 'bottom',
'title': 'Actions'
};
constructor(public $state: ng.ui.IStateService) {
this.doSomeThing = this.doSomething.bind(this);
}
doSomething() {
this.$state.go('my.state');
}
}
MyController.$inject = ['$state'];这样您就绑定了正确的This函数doSomething。因此,无论何时调用它,它的this都是一致的。
https://stackoverflow.com/questions/45187996
复制相似问题