简体   繁体   English

AngularJS 1.X ES6继承问题

[英]AngularJS 1.X ES6 inheritance issue

I have an issue trying to access to a parent property in my child Controller. 我在尝试访问子控制器中的父属性时遇到问题。 I'm using ES6 syntax and I tried with the extends syntax. 我正在使用ES6语法,并尝试使用扩展语法。

ConversationDetailController (child) 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 (parent) 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;
    });
  }
}
}

Am I doing something wrong ? 难道我做错了什么 ? Because the logs show me an empty array [] for this.conversations in the child scope. 因为日志显示了子作用域中this.conversations的空数组[] And after that I have an other error : 之后,我还有另一个错误:

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">

Thanks, Gabriel 谢谢加百列

super($rootScope, $state, $log);

calls the parent constructor. 调用父构造函数。 There you have 那里你有

this.activate();

Since this is an instance of ConversationDetailController its activate method is called, not that of ConversationListController . 由于this是一个实例ConversationDetailControlleractivate方法被调用,不就是ConversationListController And there you have 在那边

let id = this.$stateParams.id;

But $stateParams are not set yet, because: 但是尚未设置$stateParams ,因为:

super($rootScope, $state, $log); <- You are here
...
this.$stateParams = $stateParams;

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

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