AngularJS - different ways to create controllers and services, why?

后端 未结 2 395
北荒
北荒 2020-12-10 01:28

I keep seeing different examples of creating controllers and services in AngularJS and I\'m confused, can anyone explain to me the differences between the two approaches?

2条回答
  •  天命终不由人
    2020-12-10 02:12

    My preferred way of creating controllers and directives is as following:

    /**
    * SomeCoolModule.controller.js
    */
    
    (function(){
    'use strict';
    
        angular.module('app.modals.SomeCoolModule').controller('SomeCoolModuleController', SomeCoolModuleController);
    
        AddFlowCurveModalController.$inject =
            [
                '$scope',
                '$filter',
                '$log',
            ];
    
        function SomeCoolModuleController($scope, $filter, $log) {
            /* controller body goes here */
        }
    })();
    

    PS: no global namespace pollution is taking place above due to IIFE.

提交回复
热议问题