How factory works inside AngularJS?

I am browsing this blog .

enter image description here

The author pushes forward

When you use Factory , you create an object, add properties to it, and then return the same object . When you pass this service to your controller, those properties of the object will now be available in that controller through your factory.

So, in my understanding, this line

enter image description here

replaced by

enter image description here enter image description here enter image description here

as a service , an object is returned when myFactory () is called.

No problem up to this point

Now consider a fragment

<script type="text/javascript">
    var module =  angular.module('MyApp', []);

    module.factory('VaderService', function() {
        var VaderClass = function(padawan) {
            this.name = padawan;
            this.speak = function () {
                return 'Join the dark side ' + this.name;
            }
        }
        return VaderClass;
    });

    module.controller('StarWarsController', function($scope, VaderService) {
        var luke = new VaderService('luke');
        $scope.luke = luke.speak();
    });
</script>
</head>
<body ng-app="MyApp"> 
    <table ng-controller="StarWarsController">
        <tbody>
        <tr><td>{{luke}}</td></tr>
        </tbody>
    </table>
</body>

prints like

Join the dark side luke

I do not understand what is happening here with this line

var luke = new VaderService('luke');

factory VaderService ('luke'), luke , , ,

module.factory('VaderService', function() {...

-, i.e VaderClass(), . ( , javascript ).

luke VaderClass?

+4
2

, . , factory, . Angular VaderService . ( factory) - , , factory.

, Angular - - . Java factory . Angular factory , . Singleton - .

factory, init() factory, factory. .

+2

VaderClass - . factory VaderService, .

var myfact = function(name, constructor_closure){
     window[name] = constructor_closure()
}
0

All Articles