【问题标题】:AngularJS 1.X ES6 inheritance issueAngularJS 1.X ES6 继承问题
【发布时间】:2016-04-08 08:46:33
【问题描述】:

我在尝试访问子控制器中的父属性时遇到问题。 我使用的是 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">

谢谢, 加布里埃尔

【问题讨论】:

  • 我该怎么做,你有例子吗?但如果这样做,我可以在我的子控制器中修改来自我的父控制器的数据吗?比如删除对话列表中的对话?
  • 其实一开始我以为是有一个vm调用另一个vm,而不是继承。有两个不同的独立视图吗?
  • 不是分离视图,而是子视图在父视图的&lt;div ui-view&gt; 中调用。
  • 好吧,我明白了,我不会在控制器之间使用继承,每个控制器都有自己负责的视图的一部分。所以,当你使用 state.go 时,我会输入参数,{id: .., conversations: .. } 或任何你需要的。
  • 是的,我也有这个想法。但是,如果我在更新DetailController 中的对话列表时使用$state.go 方法中的参数,它不会在ListController 中更新它。对吗?

标签: javascript angularjs ecmascript-6


【解决方案1】:
super($rootScope, $state, $log);

调用父构造函数。你有

this.activate();

由于thisConversationDetailController 的实例,它的activate 方法被调用,而不是ConversationListController 的方法。你有

let id = this.$stateParams.id;

但是$stateParams还没有设置,因为:

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

【讨论】:

  • 好吧,如果我理解你所说的,super() 方法正在调用我的子控制器的 activate 方法,而不是父控制器。你有什么办法避免这种情况吗?
  • activateChild 中重命名activate 有效!非常感谢:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-27
  • 1970-01-01
  • 1970-01-01
  • 2019-03-31
  • 2019-01-25
  • 2016-05-25
  • 2011-04-05
相关资源
最近更新 更多