Last active
February 14, 2016 20:39
-
-
Save monkeymonk/82d1c121765ccecc9804 to your computer and use it in GitHub Desktop.
ES6 OOP Angular 1.x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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