How exactly does this AngularJS factory example work? Some doubts

I am brand new in AngularJS and I will learn it in a tutorial. I have doubts about using factory in Angular.

I know that factory is a template used to create objects on demand.

So, in the example there is the following code:

// Creates values or objects on demand
angular.module("myApp")     // Get the "myApp" module created into the root.js file (into this module is injected the "serviceModule" service
.value("testValue", "AngularJS Udemy")

// Define a factory named "courseFactory" in which is injected the testValue
.factory("courseFactory", function(testValue) {
    // The factory create a "courseFactory" JSON object;
    var courseFactory = {
            'courseName': testValue,    // Injected value
            'author': 'Tuna Tore',
             getCourse: function(){     // A function is a valid field of a JSON object
             alert('Course: ' + this.courseName);   // Also the call of another function
            }
          };    
    return courseFactory;       // Return the created JSON object
})

// Controller named "factoryController" in which is injected the $scope service and the "courseFactory" factory:
.controller("factoryController", function($scope, courseFactory) {
    alert(courseFactory.courseName);        // When the view is shown first show a popupr retrieving the courseName form the factory
    $scope.courseName = courseFactory.courseName;
    console.log(courseFactory.courseName);
    courseFactory.getCourse();  // Cause the second alert message
});

And this is the code associated with this factoryController inside the HTML page:

<div  ng-controller="factoryController"> 
    {{ courseName }} 
</div> 

So it's pretty clear to me that:

  • factoryController uses courseFactory factory because it is being introduced

  • The first thing that happens when I open the page is that an error message is displayed because it is called:

    alert(courseFactory.courseName);
    
  • $scope.courseName ( $scope) ** courseName courseFactory JSON).

. , factory :

.factory("courseFactory", function(testValue)

, ( , ), factory courseFactory , courseFactory JSON .

, ( Java), Java factory, , factory. , :

$scope.courseName = courseFactory.courseName;

, _ courseFactory JSON, courseFactory.

? getter factory? ( - )

+4
4

( ), ( ), factory service.

Factory vs Service

, courseFactory JSON, courseFactory.

, Angular DI ( ), factory ( ), ; factory.

Angular - , JavaScript, , , #, ++ Java

+1

.

courseFactory factory, Factory THAT. , $scope.courseName = courseFactory.courseName; , , courseFactory. , getter, courseFactory . console.log(courseFactory); , , courseFactory. , .

+1

Angular , Angular , , , , "ng" , . , .

Ref. https://docs.angularjs.org/guide/providers

+1

Factory is a Singleton service. When you enter a factory in a controller or in any other factory, you get the exact json object that you defined. It will not create a new instance every time you call it. factory is initialized only once

+1
source

Source: https://habr.com/ru/post/1619641/


All Articles