LoginSignup
3
6

More than 5 years have passed since last update.

Angular 1.x のmodule まわりの構文をまとめてみた

Last updated at Posted at 2015-12-10

AngularJS Advent Calendar 2015 11日目の投稿です。

AngularJS 機能の数は多いし、なによりそれぞれの構文が違うのが結構しんどかったりしますね。
そんな angular.module まわりの構文を改めて書き連ねてみようかと思います。

.module()

// AngularJS モジュールを作成
angular.module('myApp', [])
// 作成したモジュールの取得 
angular.module('myApp')

.config()

// minify しない or ngMin 使用
.config(function(xxxProvider){
    ...
});
// minify 対策
.config(['xxxProvider', function(xxxProvider){
    ...
]});

.run()

// minify しない or ngMin 使用
.run(function(DI){
    ...
});
// minify 対策
.run(['DI', function(DI){
    ...
}]);

.value()

// key, value (値はfunctionでもOK)
.value('key', value);
// { key, value, key2, value2 } (値はfunctionでもOK)
.value({'key': value, 'key': value2})

.constant()

// key, value (値はfunctionでもOK)
.constant('key', value);
// { key, value, key2, value2 } (値はfunctionでもOK)
.constant({'key': value, 'key': value2});

.provider()

// minify しない or ngMin 使用
.provider('MyProvider', function(DI1){
  this.xxx = yyy;
  this.$get = ['DI2', function(DI2)];
    ...
});
// minify 対策
// minify しない or ngMin 使用
.provider('MyProvider', ['DI1', function(DI1){
  this.xxx = yyy;
  this.$get = ['DI2', function(DI2)];
    ...
}]);

.factory()

// minify しない or ngMin 使用
.factory('MyFactory', function(DI){
  var MyFactory;
    ...
  return MyFactory;
});
// minify 対策
.factory('MyFactory', ['DI', function(DI){
  var MyFactory;
    ...
  return MyFactory;
}]);

.service()

// minify しない or ngMin 使用
.service('MyService', function(DI){
  this.xxx = yyy;
    ...
});
// minify 対策
.service('MyService', function(DI){
  this.xxx = yyy;
    ...
});

.controller()

// minify しない or ngMin 使用
.controller('MyController', function(DI){
    ...
);
// minify 対策
.controller('MyController', ['DI', function(DI){
    ...
]);
// $inject で DI
var MyController = function(DI) {
    ...
};
MyController.$inject = ['DI'];
.controller('MyController', MyController);

.directive()

// minify しない or ngMin 使用
.directive('MyDirective', function(DI){
  return {
    require: *1,
    restrict: *1,
    transclude: *1,
    scope: *1,
    link: function(scope, element, attrs, controllers) {
      ...
    },
    templateUrl: *2,
    template: *2
  };
);
  1. 任意オプション

  2. 'restrict' が default or 'E' 含む場合、どちらかは必須

. どちらかは必須 (コメント頂いて修正)

// minify 対策
.directive('MyDirective', ['DI', function(DI){
    ... // 内容は上と同じ
]);

.component()

.component('MyComponent', {
  bindings: *3,
  controller: *3,
  controllerAs: *3,
  restrict: *3,
  transclude: *3,
  isolate: *3,
  templateUrl: *4,
  template: *4
});
  1. 任意オプション

  2. *2と違って任意オプション。デフォルトは ''

感想

他にもあれば教えてください。

3
6
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6