AngularJS - Newable Factory Service ex.

by gavinfoley

HTML

<div ng-app="ExampleApplication">
    <div ng-controller="InstanceController"></div>
</div>

JavaScript

var ExampleApplication = angular.module('ExampleApplication', []);


ExampleApplication.factory('InstancedService', function () {

    function Instance(name, type) {
        this.name = name;
        this.type = type;
    }

    return {
        Instance: function (name, type) {
            return new Instance(name, type);
        }
    };

    //return {
    //    Instance: Instance
    //};

});


ExampleApplication.controller('InstanceController', function ($scope, InstancedService) {
    var instanceA = new InstancedService.Instance('A', 'string'),
        instanceB = InstancedService.Instance('B', 'object');
    console.log("Instances are equal:", angular.equals(instanceA, instanceB));

});