你的位置:首页 > 软件开发 > Java > 在AngularJS中使用ES6

在AngularJS中使用ES6

发布时间:2015-12-09 17:00:05
本篇记录一些AngularJS结合使用ES6的各种写法。ES6中module的导出导入 class MainController { constructor(searchService){ this.searchService = searchService; } ...

 

本篇记录一些AngularJS结合使用ES6的各种写法。ES6中module的导出导入

 

class MainController {  constructor(searchService){    this.searchService = searchService;  }    search(){    this.searchService      .fetch(this.searchTerm)      .then(response => {        this.items = resposne.data.items;      })  }}export {MainController}
但,以上的写法也不是最理想的。最好是写在类中。

 

class ThingFactory{  constructor($timeout){    this.$timeout = $timeout;  }    createThing(){    return this.$timeout(() => new Thing(),1000);  }}ThingFactory.$inject = ['$timeout'];
如果改成这样:

 

compile(tElement){  tElement.css('position','absolute');  return (scope, element) => {    this.$interval(() => this.move(element),1000);  };}
还可以这么写:

 

compile(tElement){  tElement.css('position','absolute');  return this.link.bind(this);}link(scope, element){  this.$interval(() => this.move(element),1000);}
Michael Bromley还专门写了一个帮助方法,在这里。大致这样使用:

 

class MyAngularComponent {  /*@ngInject*/  constructor(dependency1, dependency2) {    this.dependency1 = dependency1;    // stuff happens here  }  someMethods() {    this.dependency1.doThatThing();    // more stuff here  }}register('app')  .controller('MyController', MyAngularComponent)  .service('myService', MyAngularComponent)  .provider('myOtherService', MyAngularComponent)  .factory('myFactory', MyAngularComponent)  .directive('myDirective', MyAngularComponent);

 


原标题:在AngularJS中使用ES6

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。