控制器使用工厂函数创​​建? [英] controllers are created using a factory function?

查看:191
本文介绍了控制器使用工厂函数创​​建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来angularJS和经历的角度docs.I跨越这条线来创建 控制器使用工厂函数

我试图找到这意味着什么,发现什么是工厂及服务和服务提供者,但不适合在这里。
 如何使用一个工厂函数创​​建控制器?

请解释一下什么是的工厂在这方面的意义


解决方案

你是从你指的是previous部分中缺少关键的报价是这样的:

首先,它包含一个所谓的一个新的JavaScript文件控制器。更确切地说,该文件包含创建实际控制实例的构造函数。<​​/ P>

如果这是一个实际的角度工厂,它会更有意义。但是,控制器实例就像角工厂,服务和供应商。

有一个工厂,其实是一个Javascript的设计模式,也许看完这会更有意义。

有关的控制器的工作,该实例必须存在用于双向绑定到能够发生。因此,基本上,控制器的一个实例被创建。角控制器页解释得很好用:

当一个控制器连接到通过NG-控制器指令中的DOM,角将实例化一个新的控制器对象,使用指定的控制器的构造函数,一个新的子范围,将可作为注射参数传递给控制器​​的构造函数为$范围。 这里的链接。

在控制器的情况下,虽然,你会在$范围,而不是'这个'最有可能的商店物品。因此,他们从工厂这种方式,因为他们没有自己的返回的访问实例并而不是通过$范围或本。

绑定其视图属性独立的控制器

需要明确的是,我不是说他们指的是角工厂。我相信,这个措辞的原因是联系在一起的同样的措辞的服务工厂功能

应用程序开发人员可以自由通过注册该服务的名称和服务工厂函数,用角模块定义自己的服务。

服务工厂函数生成的单个对象或函数重新presents服务到应用程序的其余部分。服务返回的对象或函数被注入到指定的服务的依赖任何组件(控制器,服务,过滤器或指令)。

他们给这个例子:

  VAR Mymodule中= angular.module('Mymodule中',[]);
myModule.factory('服务ID',函数(){
  VAR shinyNewServiceInstance;
  //工厂函数体构造shinyNewServiceInstance
  返回shinyNewServiceInstance;
});

所以,当你看到他们说从工厂函数创​​建的,他们使用的是以下模式只是说:

  .controller('PersonCtrl',[
  PersonFactory
  功能(PersonFactory){
    this.name ='汤姆';
    的console.log(PersonFactory.name); //将打印'汤姆'
}]);.factory(PersonFactory,[
  功能(){
    返回{
      名称:'汤姆'
    };
}]);

我希望帮助,或者也许有人可以在我的解释更简洁和修改该描述。

I am new to angularJS and going through angular docs.I came across this line controllers are created using a factory function

I tried to find what that means and found what is factory and service and providers but that does not fit here. How controllers are created using a factory function?

Please explain what is the meaning of factory in this context.

解决方案

The key quote you are missing from the previous section you are referring to is this:

"First, there is a new JavaScript file that contains a so-called "controller". More exactly, the file contains a constructor function that creates the actual controller instance. "

If this were an actual angular factory, it would make more sense. But, controllers are instances just like Angular factories, services, and providers.

A factory, is actually a Javascript design pattern, maybe reading here it will make more sense.

For the controller to work, the instance must exist for the two-way binding to be able to take place. So basically, an instance of the controller is created. The angular controller page explains it well with:

"When a Controller is attached to the DOM via the ng-controller directive, Angular will instantiate a new Controller object, using the specified Controller's constructor function. A new child scope will be available as an injectable parameter to the Controller's constructor function as $scope." Here's the link.

In the event of controllers though, you would most likely store items on the $scope and not 'this'. So they separate controllers from factories this way as they do not return an accessible instance of themselves and instead bind their properties to the view through $scope or 'this'.

TO BE CLEAR, I'm not saying that they are referring to Angular factories. I believe the reason for this phrasing is tied to the same wording for the service factory function:

"Application developers are free to define their own services by registering the service's name and service factory function, with an Angular module.

The service factory function generates the single object or function that represents the service to the rest of the application. The object or function returned by the service is injected into any component (controller, service, filter or directive) that specifies a dependency on the service."

They give this example:

var myModule = angular.module('myModule', []);
myModule.factory('serviceId', function() {
  var shinyNewServiceInstance;
  // factory function body that constructs shinyNewServiceInstance
  return shinyNewServiceInstance;
});

So when you see them say created from a factory function, they are just saying using the following pattern:

.controller('PersonCtrl', [
  'PersonFactory'
  function(PersonFactory) {
    this.name = 'Tom';
    console.log(PersonFactory.name); //would print 'Tom'
}]);

.factory("PersonFactory", [
  function () {
    return {
      name: 'Tom'
    };
}]);

I hope that helps, or maybe someone could be more concise in my explanation and edit this description.

这篇关于控制器使用工厂函数创​​建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆