1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AngularJSに関数登録時、newされる・newされないケースを調べてみた

Last updated at Posted at 2015-06-18

テストコード

check.html
<div ng-controller="NewCheckController as vm">
  <ul>
    <li><new-check-directive></new-check-directive></li>
    <li><strong>{{vm.factoryMsg}}</strong></li>
    <li><strong>{{vm.serviceMsg}}</strong></li>
  </ul>
</div>
check.js
//config
;(function(){
  angular.module('app').config(config);
  function config(){
    console.log('Config');
    console.log(this.isNew ? 'newされる' : 'newされない');
  }
  config.prototype.isNew = true;
})();

//controller
;(function(){
  angular.module('app').controller('NewCheckController', Controller)
  function Controller(
    NewCheckFactory,
    NewCheckService
  ){
    console.log('Controller');
    console.log(this.isNew ? 'newされる' : 'newされない');
    this.factoryMsg = NewCheckFactory.getMessage();
    this.serviceMsg = NewCheckService.getMessage();
  }  
  Controller.prototype.isNew = true;
})();

//directive
;(function(){
  angular.module('app').directive('newCheckDirective', directiveFactory);
  function directiveFactory(){
    console.log('Directive');
    console.log(this.isNew ? 'newされる' : 'newされない');
    return new Directive('directive()にはdirectiveのfactory関数を指定します');
  }
  directiveFactory.prototype.isNew = true;

  function Directive(msg){
    this.msg = msg;
  }
  Directive.prototype = {
    template: function(){
      return '<strong>'+this.msg+'</strong>'
    }
  }
})();

//factory
;(function(){
  angular.module('app').factory('NewCheckFactory', serviceFactory)
  function serviceFactory(){
    console.log('Factory');
    console.log(this.isNew ? 'newされる' : 'newされない');
    return new Service('factory()にはserviceのfactory関数を指定します');
  }  
  serviceFactory.prototype.isNew = true;

  function Service(msg){
    this.msg = msg;
  }
  Service.prototype = {
    getMessage: function(){
      return this.msg;
    }
  }
})();

//service
;(function(){
  angular.module('app').service('NewCheckService', Service)
  function Service(){
    console.log('Service');
    console.log(this.isNew ? 'newされる' : 'newされない');
    this.msg = 'service()にはserviceのコンストラクタを指定します'
  }
  Service.prototype = {
    isNew: true,
    getMessage: function(){
      return this.msg;
    }
  }
})();

結果

newcheck.png

  • Config
    • newされない
  • Directive
    • newされない
  • Factory
    • newされない
  • Service
    • newされる
  • Controller
    • newされる
1
2
0

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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?