为什么我需要在我的应用中注入我想要的控制器?

时间:2021-04-06 23:20:54

I am reading the source code for an app which reads as follows:

我正在阅读一个应用程序的源代码,其内容如下:

angular.module('graffio', [
  'graffio.signupController',
  'graffio.loginController',
  'graffio.mainController',
  'ui.router'
])

I have quite a few questions! So much confusion...

我有很多问题要问!如此多的混乱……

  1. How is code loading these controllers which are defined later in the code?
  2. 代码如何加载这些稍后在代码中定义的控制器?
  3. Why do I even need to state which controllers I want in my app? Why can't I just have all the ones I declare?
  4. 为什么我需要在app中声明我想要的控制器?为什么我不能把我申报的所有东西都拿到?
  5. Why does the angular documentation use the word 'injectable' so much? Isn't injectable just the same as require?
  6. 为什么角文档中使用“注射”这个词这么多?注射不是和要求一样吗?

1 个解决方案

#1


2  

With Angular, you can group as much or as little code into a module as you like. And you can compose modules together, like the author of the app you are looking at has done. The final module will have all of the services, config blocks, routes, controllers, directives, filters and so on that are in all of the modules it depends on as well as its own module.

有了角,您可以将尽可能多或少的代码分组到一个模块中。你可以将模块组合在一起,就像你正在看的应用的作者已经完成的那样。最后一个模块将包含它所依赖的所有模块以及它自己的模块中的所有服务、配置块、路由、控制器、指令、过滤器等等。

This author has chosen to put each controller into its own module. Which is why the main module needs to depend on each of those modules. In my opinion, this seems like overkill, but it is what has been done, and all you need to do is understand it, not agree with it.

作者选择将每个控制器放在自己的模块中。这就是为什么主模块需要依赖于每个模块的原因。在我看来,这似乎有点过头了,但事实就是这样,你所需要做的就是理解它,而不是同意它。

In answer to your other questions:

回答你的其他问题:

How is code loading these controllers which are defined later in the code?

When your code first runs, all the modules will be declared, and populated with directives, routes, controllers, services and so on. Nothing gets used yet. So long as when the code you have above is run, the other modules have already been declared then everything is fine (this may be done by a build process such as a Grunt task).

当代码第一次运行时,将声明所有模块,并用指令、路由、控制器、服务等填充它们。还没有被使用。只要运行上面的代码,其他模块就已经声明,那么一切都没问题(这可以通过构建过程完成,比如一个繁重的任务)。

Then, when the document.ready event occurs, Angular looks through your HTML for an ng-app directive that says which module to load as your application. It then does what it calls the "bootstrap" process for that module.

然后,当文档。准备就绪事件发生时,角查看HTML,寻找一个ng-app指令,指示将哪个模块作为应用程序加载。然后它会执行它所称的那个模块的“引导程序”过程。

Why do I even need to state which controllers I want in my app? Why can't I just have all the ones I declare?

Because this author has put each controller in their own module. If you put all the controllers you want into one module, then you don't need to declare them like that.

因为作者把每个控制器都放在自己的模块中。如果将所有的控制器放入一个模块中,则不需要像这样声明它们。

Why does the angular documentation use the word 'injectable' so much? Isn't injectable just the same as require?

Sort of. It is similar in that you are asking for a dependency by name and then using it.

排序的。类似的是,您按名称请求一个依赖项,然后使用它。

It is different in that with require you can't typically modify what value is retrieved for a given dependency at runtime. With dependency injection, you can swap or modify dependencies at run time if you so choose (before your app starts properly and the dependencies are injected into the code using them).

与require不同的是,您通常不能在运行时修改为给定依赖项检索的值。使用依赖项注入,您可以在运行时交换或修改依赖项(在应用程序正常启动并使用它们将依赖项注入代码之前)。

This is perfect for testing, so that you can use a mock (fake) version of a dependency for testing purposes, instead of using the normal version. Think of a service that makes a call to get some data from the server and returns a promise. You can just create a mock version of that service that doesn't call to the server, just returns some test data immediately. This makes your unit tests fast, and means you don't need to be running your web server for them to work.

这对于测试来说是完美的,因此您可以将依赖项的模拟(伪)版本用于测试目的,而不是使用常规版本。考虑一个调用从服务器获取一些数据并返回一个承诺的服务。您可以创建一个不调用服务器的服务的模拟版本,直接返回一些测试数据。这使您的单元测试快速,并且意味着您不需要运行web服务器才能让它们工作。

#1


2  

With Angular, you can group as much or as little code into a module as you like. And you can compose modules together, like the author of the app you are looking at has done. The final module will have all of the services, config blocks, routes, controllers, directives, filters and so on that are in all of the modules it depends on as well as its own module.

有了角,您可以将尽可能多或少的代码分组到一个模块中。你可以将模块组合在一起,就像你正在看的应用的作者已经完成的那样。最后一个模块将包含它所依赖的所有模块以及它自己的模块中的所有服务、配置块、路由、控制器、指令、过滤器等等。

This author has chosen to put each controller into its own module. Which is why the main module needs to depend on each of those modules. In my opinion, this seems like overkill, but it is what has been done, and all you need to do is understand it, not agree with it.

作者选择将每个控制器放在自己的模块中。这就是为什么主模块需要依赖于每个模块的原因。在我看来,这似乎有点过头了,但事实就是这样,你所需要做的就是理解它,而不是同意它。

In answer to your other questions:

回答你的其他问题:

How is code loading these controllers which are defined later in the code?

When your code first runs, all the modules will be declared, and populated with directives, routes, controllers, services and so on. Nothing gets used yet. So long as when the code you have above is run, the other modules have already been declared then everything is fine (this may be done by a build process such as a Grunt task).

当代码第一次运行时,将声明所有模块,并用指令、路由、控制器、服务等填充它们。还没有被使用。只要运行上面的代码,其他模块就已经声明,那么一切都没问题(这可以通过构建过程完成,比如一个繁重的任务)。

Then, when the document.ready event occurs, Angular looks through your HTML for an ng-app directive that says which module to load as your application. It then does what it calls the "bootstrap" process for that module.

然后,当文档。准备就绪事件发生时,角查看HTML,寻找一个ng-app指令,指示将哪个模块作为应用程序加载。然后它会执行它所称的那个模块的“引导程序”过程。

Why do I even need to state which controllers I want in my app? Why can't I just have all the ones I declare?

Because this author has put each controller in their own module. If you put all the controllers you want into one module, then you don't need to declare them like that.

因为作者把每个控制器都放在自己的模块中。如果将所有的控制器放入一个模块中,则不需要像这样声明它们。

Why does the angular documentation use the word 'injectable' so much? Isn't injectable just the same as require?

Sort of. It is similar in that you are asking for a dependency by name and then using it.

排序的。类似的是,您按名称请求一个依赖项,然后使用它。

It is different in that with require you can't typically modify what value is retrieved for a given dependency at runtime. With dependency injection, you can swap or modify dependencies at run time if you so choose (before your app starts properly and the dependencies are injected into the code using them).

与require不同的是,您通常不能在运行时修改为给定依赖项检索的值。使用依赖项注入,您可以在运行时交换或修改依赖项(在应用程序正常启动并使用它们将依赖项注入代码之前)。

This is perfect for testing, so that you can use a mock (fake) version of a dependency for testing purposes, instead of using the normal version. Think of a service that makes a call to get some data from the server and returns a promise. You can just create a mock version of that service that doesn't call to the server, just returns some test data immediately. This makes your unit tests fast, and means you don't need to be running your web server for them to work.

这对于测试来说是完美的,因此您可以将依赖项的模拟(伪)版本用于测试目的,而不是使用常规版本。考虑一个调用从服务器获取一些数据并返回一个承诺的服务。您可以创建一个不调用服务器的服务的模拟版本,直接返回一些测试数据。这使您的单元测试快速,并且意味着您不需要运行web服务器才能让它们工作。