[英]AngularJS 1.X ES6 inheritance issue
我在尝试访问子控制器中的父属性时遇到问题。 我正在使用ES6语法,并尝试使用扩展语法。
ConversationDetailController(子级)
import { ConversationListController} from './conversation-list.controller';
export class ConversationDetailController extends ConversationListController {
constructor ($rootScope, $state, $stateParams, $mdSidenav, $mdDialog, $log, moment, ConversationService, algolia) {
'ngInject';
super($rootScope, $state, $log);
this.conversation = {};
this.newMessageText = "";
this.ConversationService = ConversationService;
this.$stateParams = $stateParams;
this.$log = $log;
this.$mdSidenav = $mdSidenav;
this.$mdDialog = $mdDialog;
this.moment = moment;
this.algolia = algolia;
this.activate();
}
activate() {
this.$log.debug(this.conversations); // Here I want to access this.conversations from the parent scope.
let id = this.$stateParams.id;
return this.ConversationService.getConversationByID(parseInt(id))
.success((conversation) => {
this.conversation = conversation;
this.conversation.messages.forEach((item) => {
item.relativeDate = this.moment(item.createdAt).clone().fromNow();
return item;
});
this.$log.debug(this.conversation);
return conversation;
})
.error((err) => {
return err;
});
}
}
ConversationListController(父级)
export class ConversationListController {
constructor ($rootScope, $state, $log, ConversationService) {
'ngInject';
this.conversations = [];
this.ConversationService = ConversationService;
this.$state = $state;
this.$log = $log;
this.showSearch = false;
this.role = $rootScope.role;
this.currentUser = $rootScope.currentUser;
this.activate();
}
activate() {
if (this.currentUser.doctor) {
this.ConversationService.getConversationList(this.currentUser.doctor)
.success((conversations) => {
this.conversations = conversations;
this.conversations[0].active = true;
this.$state.go('app.conversations.detail', {id: this.conversations[0].id});
return conversations;
})
.error((err) => {
return err;
});
} else {
this.ConversationService.getConversationList()
.success((conversations) => {
this.conversations = conversations;
this.conversations[0].active = true;
this.$state.go('app.conversations.detail', {id: this.conversations[0].id});
return conversations;
})
.error((err) => {
return err;
});
}
}
}
难道我做错了什么 ? 因为日志显示了子作用域中this.conversations
的空数组[]
。 之后,我还有另一个错误:
angular.js:12722 TypeError: Cannot read property 'id' of undefined
at ConversationDetailController.activate (http://localhost:3000/app/index.module.js:602:34)
at ConversationDetailController.ConversationListController (http://localhost:3000/app/index.module.js:504:11)
at new ConversationDetailController (http://localhost:3000/app/index.module.js:582:95)
at invoke (http://localhost:3000/bower_components/angular/angular.js:4535:17)
at Object.instantiate (http://localhost:3000/bower_components/angular/angular.js:4543:27)
at http://localhost:3000/bower_components/angular/angular.js:9395:28
at http://localhost:3000/bower_components/angular-ui-router/release/angular-ui-router.js:4081:28
at invokeLinkFn (http://localhost:3000/bower_components/angular/angular.js:9039:9)
at nodeLinkFn (http://localhost:3000/bower_components/angular/angular.js:8533:11)
at compositeLinkFn (http://localhost:3000/bower_components/angular/angular.js:7929:13) <div ui-view="" layout="column" class="w-100 ng-scope">
谢谢加百列
super($rootScope, $state, $log);
调用父构造函数。 那里你有
this.activate();
由于this
是一个实例ConversationDetailController
其activate
方法被调用,不就是ConversationListController
。 在那边
let id = this.$stateParams.id;
但是尚未设置$stateParams
,因为:
super($rootScope, $state, $log); <- You are here
...
this.$stateParams = $stateParams;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.