Skip to content

Instantly share code, notes, and snippets.

@mori-dev
Created August 14, 2015 04:01
Show Gist options
  • Select an option

  • Save mori-dev/5e4f96093faf7d31725f to your computer and use it in GitHub Desktop.

Select an option

Save mori-dev/5e4f96093faf7d31725f to your computer and use it in GitHub Desktop.
AngularJS のディレクティブのコントローラでインジェクトするサービスを動的に決めるには ref: http://qiita.com/mori-dev@github/items/1dbb3a7d96ce6a0d4549
<my-hoge filter="AaaFilter"></my-hoge>
class HogeDirectiveController {
constructor($injector) {
this.$injector = $injector;
}
setFilter(filter) {
this.filter = this.$injector.get(filter);
}
}
HogeDirectiveController.$inject = ['$injector'];
function HogeDirective($parse) {
return {
restrict: 'E',
controller: HogeDirectiveController,
controllerAs: 'dctrl',
scope: {},
link: function (scope, element, attrs, controller) {
scope.dctrl.setFilter(attrs.filter);
},
template: `
<input ng-model="dctrl.filter.piyopiyo">
`
};
}
angular.module('MyAdmin').directive('myHoge', HogeDirective)
export default HogeDirective;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment