最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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?

app.service('reverseService', function() {
    this.reverse = function(name) {
        return name.split("").reverse().join("");
    };
});

app.factory('reverseService', function() {
    return {
        reverse : function(name) {
            return name.split("").reverse().join("");
        }
    }
});

And also a controller example:

function ExampleCtrl($scope) {
    $scope.data = "some data";
}

app.controller("ExampleCtrl", function($scope) {
    $scope.data = "some data";
}

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?

app.service('reverseService', function() {
    this.reverse = function(name) {
        return name.split("").reverse().join("");
    };
});

app.factory('reverseService', function() {
    return {
        reverse : function(name) {
            return name.split("").reverse().join("");
        }
    }
});

And also a controller example:

function ExampleCtrl($scope) {
    $scope.data = "some data";
}

app.controller("ExampleCtrl", function($scope) {
    $scope.data = "some data";
}
Share Improve this question edited Dec 9, 2013 at 15:20 C1pher 1,9726 gold badges34 silver badges52 bronze badges asked Jan 6, 2013 at 17:18 NeilNeil 8,1114 gold badges57 silver badges77 bronze badges 3
  • 2 A question very similar to stackoverflow.com/q/13362921/1418796 – pkozlowski.opensource Commented Jan 6, 2013 at 17:30
  • 1 I couldn't find the answer, that's why I raised my own question, given the title of that question and my question, I believe my question should stand to aid others. – Neil Commented Jan 6, 2013 at 17:57
  • 1 See also stackoverflow.com/questions/13762228/… – Mark Rajcok Commented Jan 8, 2013 at 4:02
Add a comment  | 

2 Answers 2

Reset to default 20

The first one will pollute the global namespace, which is not what you want in the long run.

function ExampleCtrl($scope){
    $scope.data = "some data";
}

The second one scopes the Controller to that module instance. It makes it also injectable. Better still is using the array notation (as below), since this will survive minification.

app.controller("ExampleCtrl", ['$scope', function($scope){
    $scope.data = "some data";
}]);

The difference between an (angular) service and factory seems quite small. A service wraps a factory, which uses $injector.instantiate to initialize the service.

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论