Skip to content

Instantly share code, notes, and snippets.

@monkeymonk
Last active February 14, 2016 20:39
Show Gist options
  • Save monkeymonk/82d1c121765ccecc9804 to your computer and use it in GitHub Desktop.
Save monkeymonk/82d1c121765ccecc9804 to your computer and use it in GitHub Desktop.
ES6 OOP Angular 1.x
import WatheverDirective from './wathever.directive';
import WatheverController from './wathever.controller';
import WatheverService from './wathever.service';
export default angular.module('wathever', [])
.directive('wathever', WatheverDirective)
.controller('WatheverController', WatheverController.factory)
.service('WatheverService', WatheverService.factory);
export default class WhateverController {
constructor($scope, WatheverService) {
this.$scope = $scope;
this.WatheverServer = WatheverService;
this.something = null;
this.$scope.$watch(() => this.something, (something) => {
if (something) {
this.WatheverService.get(something)
.then(console.log)
.catch(console.error);
}
});
}
static factory($scope, WatheverService) {
WhateverController.$inject = ['$scope', 'WatheverService']
return new WhateverController($scope, WatheverService);
}
}
export default WhateverDirective {
constructor(WatheverService) {
return {
restrict: 'E',
// scope: {}, // angular < 1.5
bindToController: {}, // angular >= 1.5
// bindings: {}, // with .component()
template: `<div ng-transclude></div>`,
transclude: true,
controller() {},
link(scope, element, attrs, ctrl, transclude) {
transclude(scope, (clone, scope) => element.find('[ng-transclude]').replaceWith(clone));
WatheverService.get('something')
.then(console.log)
.catch(console.error);
},
};
}
static factory(WatheverService) {
WatheverDirective.$inject = ['WatheverService'];
return new WatheverDirective(WatheverService);
}
}
export default class WatheverService {
constructor($http) {
this.$http = $http;
this.debug = true;
}
get(something) {
return this.$http.get(`somwhere/${something}`);
}
static factory() {
WatheverService.$inject = ['$http'];
return new WatheverService($http);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment