这个 AngularJS 工厂示例究竟是如何工作的?一些疑惑

How exactly works this AngularJS factory example? Some doubts

我完全是 AngularJS 的新手,我正在学习教程。我对 factory 在 Angular.

中的使用有疑问

我知道工厂是一种用于根据请求创建对象的模式。

所以在例子中有如下代码:

// 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
});

这是与 factoryController 控制器关联的代码,在 HTML 页面内:

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

所以我很清楚:

这是我的第一个疑问。我知道工厂定义为:

.factory("courseFactory", function(testValue)

我认为这意味着(如果我做错了断言请纠正我)我的工厂被命名为 courseFactory 并且基本上是一个返回 courseFactory 的函数 JSON 对象。

所以这让我有些困惑(我来自 Java),因为在 Java 中我调用了一个工厂 class 并且我获得了一个由工厂创建的对象的实例.但在这种情况下,在我看来,这样做:

$scope.courseName = courseFactory.courseName;

在我看来 courseName 是由 courseFactory JSON 对象直接检索的,我没有使用课程工厂.

具体是如何运作的?为什么我不在工厂上调用一些getter? (或类似的东西)

简单。

当您 return 工厂语句中的 courseFactory 对象时,控制器中注入的 courseFactory 成为该对象。因此,当您在控制器中执行 $scope.courseName = courseFactory.courseName; 时,您实际上是在引用作为 courseFactory 注入到控制器中的 returned 对象。所以你不需要 getter,因为你控制器中的 courseFactory 已经是对象。您可以在控制器中执行 console.log(courseFactory); 以查看 courseFactory 是什么。希望这有帮助。

When an Angular application starts with a given application module, Angular creates a new instance of injector, which in turn creates a registry of recipes as a union of all recipes defined in the core "ng" module, application module and its dependencies. The injector then consults the recipe registry when it needs to create an object for your application.

参考。 https://docs.angularjs.org/guide/providers

为了简化事情有两种已知的方法来创建在所有状态中可用的函数(和方法)(将其视为在全局范围内创建函数),它们是工厂和服务。

你可能想先阅读这篇文章Factory vs Service

it seems to me that the courseName is retrieved directly by the courseFactory JSON object and I am not using the courseFactory.

实际上这是部分正确的,因为 Angular 使用 DI 概念(依赖注入),你将不得不注入你的工厂(默认情况下 returns 一个对象,以及作为属性的相应函数) ,如果你不这样做;您将无法使用您的工厂对象方法。

Angular 不是魔术,只是大多数人没有意识到 JavaScript 的最基本概念,特别是来自不同编程环境(如 C#、C++ 或 Java

Factory 是一个单例服务。当您在控制器或任何其他工厂中注入工厂时,您会得到您定义的确切 json 对象。它不会在您每次调用它时创建任何新实例。工厂只初始化一次