【发布时间】:2016-05-22 08:30:28
【问题描述】:
我正在为我的控制器试验 ES6 类。
以前如果我用 ui-router 监听 rootScope 事件,我可以这样做:
$rootScope.$on('stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
console.log(toState)
}
并且 toState 将很容易访问。
我的 ES6 控制器目前看起来像这样:
class NavbarController {
constructor ($rootScope) {
this.name = 'navbar'
this.$rootScope = $rootScope
this.$rootScope.$on('stateChangeStart', this.activeNav())
}
activeNav () {
console.log('This is called')
return (event, toState, toParams) => {
console.log('This is never called')
}
}
}
NavbarController.$inject = ['$rootScope']
export default NavbarController
从我读过的帖子中,我认为我在正确的轨道上但是我返回的函数没有被调用,我不知道如何访问事件参数。
那么在 ES6 角度控制器中处理事件的正确方法是什么?
【问题讨论】:
-
您的代码不是有效的 JS。首先修复所有语法问题(括号太多,缺少箭头等。我还会在您的语句末尾添加分号。
-
为语法错误道歉,我冲了sn-p。他们现在是正确的。我遵循standardjs,因此没有分号