AngularJS& ES6:服务未按预期记录

时间:2018-02-04 20:44:37

标签: angularjs ecmascript-6

我无法让我的AngularJS服务与ES6一起运行。这是超级简化但我仍然无法让它工作!

代码不会抛出任何错误。

joinWith

// basic template
const template = '<h1>Super Bowl Quarterbacks (logged)</h1>';

// AppStore class
class AppStore {
  constructor($log) {
    this.$log = $log;
  }

  logName() {
    this.$log.log('Tom Brady');
    this.$log.log('Nick Foles');
  }
}

// Controller class
class Controller {
  constructor(AppStore) {
    AppStore.logName();
  }
}

// Component object
const component = {
  bindings: {},
  template: template,

// Per estus post, this solved my initial issue of not getting logging to work
  controller: Controller
};

// app component
angular.module('app',[])
.component('app', component)

// Per estus post, this solved unknown provider error
.service('AppStore', AppStore);

编辑:更新的代码和jsbin。我记下了为了让它发挥作用而改变了什么。

1 个答案:

答案 0 :(得分:2)

ES6含糖语法

const component = {
  /*...*/
  Controller
};

对应于ES5

const component = {
  /*...*/
  Controller: Controller
};

虽然组件应具有controller属性。

应该是:

const component = {
  bindings: {},
  template,
  controller: Controller
};