angular 手动注入

Earlier we looked at various angular form features and its validation. In this post, We will discuss about angular initialization process and how to manually bootstrap an angular application if needed.

之前,我们研究了各种角形特征及其验证。 在本文中,我们将讨论角度初​​始化过程以及如何在需要时手动引导角度应用程序。

自动引导一个Angular应用程序 (Automatically Bootstrapping an Angular Application)

Let us have a look at the auto-bootstrap process in angular application that we have used so far. The following code shows the structure of the code, which we have written so far.

让我们看一下到目前为止已经使用过的角度应用程序中的自动引导过程。 以下代码显示了我们到目前为止编写的代码结构。

  • The script tag is placed at the bottom of the page to improve application load time. By placing script at the end, we can ensure that HTML loading is not blocked by angular.js script loading.脚本标签位于页面底部,以缩短应用程序加载时间。 通过将脚本放在最后,我们可以确保angular.js脚本加载不会阻止HTML加载。
  • The ng-app directive can be placed anywhere in the application. That’s going to be the entry point of the application or angular will automatically bootstrap the application when it sees the ng-app directive.ng-app指令可以放在应用程序中的任何位置。 那将是应用程序的入口点,或者当看到ng-app指令时,angular将自动引导应用ng-app
<!doctype html>
<html lang="en" ng-app="appName"><head><meta charset="UTF-8"><title>AngularJS Application</title></head><body><!-- other code goes here --><script type="text/javascript" src="angular.js"></script></body>
</html>

初始化过程 (Initialization Process)

The following processes takes place in every angular application:

在每个角度应用程序中都会发生以下过​​程:

  1. Angular initializes automatically when DOM content is completely loaded or when the angular.js script is evaluated.当DOM内容完全加载或评估angular.js脚本时,Angular会自动初始化。
  2. Angular looks for the ng-app directive, if found then it loads the module associated with the ng-app directive.Angular会查找ng-app指令,如果找到,它将加载与ng-app指令关联的模块。
  3. Then an application injector is created.然后创建一个应用程序注入器 。
  4. This will retrieve object instances, instantiate types, invoke methods, and load modules.这将检索对象实例,实例化类型,调用方法和加载模块。
  5. Compile the DOM elements treating the element containing ng-app directive as the root of the application.编译DOM元素,将包含ng-app指令的元素视为应用程序的根。

手动引导Angular应用程序 (Manually Bootstrapping an Angular Application)

The automatic initialization will not work with asynchronously loaded data that need to perform an operation before Angular compiles a page. The angular will not wait until the loading of data if we rely on auto-initialization process. Therefore, in this scenario you need to have a better control over the initialization process.

自动初始化将不适用于需要在Angular编译页面之前执行操作的异步加载的数据。 如果我们依靠自动初始化过程,角度将不等到数据加载。 因此,在这种情况下,您需要更好地控制初始化过程。

Angular provides a method to control the bootstrap process programmatically or we say manually using angular.boostrap() method.

Angular提供了一种以编程方式控制引导过程的方法,或者我们说是使用angular.boostrap()方法手动进行angular.boostrap()

You should not use the ng-app directive if you use angular.bootstrap method.

如果使用angular.bootstrap方法,则不应使用ng-app指令。

使用angular.bootstrap()示例 (Using angular.bootstrap() Example)

The following example demonstrates how to use angular.bootstrap method to bootstrap an angular application.

以下示例演示如何使用angular.bootstrap方法来引导角度应用程序。

We will first create a module and define a controller for our application.

我们将首先创建一个模块并为我们的应用程序定义一个控制器。

app.js

app.js

var app = angular.module('myApp', []);app.controller('MessageCtrl', function($scope) {$scope.message = 'Angular Bootstrap - Successful';});

The following script is used to bootstrap the application. We will pass our module name as the second parameter to the angular.bootstrap method. the first parameter is the document itself. When it is ready, we will bootstrap our application using this method.

以下脚本用于引导应用程序。 我们将把模块名称作为第二个参数传递给angular.bootstrap方法。 第一个参数是文档本身。 准备就绪后,我们将使用此方法引导我们的应用程序。

bootstrapApp.js

bootstrapApp.js

angular.element(document).ready(function() {angular.bootstrap(document, ['myApp']);
});

Now we can include these two files in the HTML page and run our application.

现在,我们可以将这两个文件包含在HTML页面中并运行我们的应用程序。

index.html

index.html

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><title>AngularJS Bootstrap</title></head><body><div ng-controller="MessageCtrl"><p>{{ message }}!</p></div><script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script><script src="app.js"></script><script src="bootstrapApp.js"></script></body>
</html>

You will see the following output on your browser.

That’s all about initialization process and we will see more features in the coming posts.

您将在浏览器中看到以下输出。

这些都与初始化过程有关,我们将在以后的文章中看到更多功能。

翻译自: https://www.journaldev.com/8060/manually-bootstrapping-an-angular-application

angular 手动注入

angular 手动注入_手动引导Angular JS应用程序相关推荐

  1. angular 模块构建_通过构建全栈应用程序学习Angular 6

    angular 模块构建 Angular 6 is out! The new features include better performance, new powerful CLI additio ...

  2. angular过滤字符_如何使用Angular和Azure计算机视觉创建光学字符读取器

    angular过滤字符 介绍 (Introduction) In this article, we will create an optical character recognition (OCR) ...

  3. angular 自定义组件_如何创建Angular 6自定义元素和Web组件

    angular 自定义组件 by Prateek Mishra 通过Prateek Mishra 如何创建Angular 6自定义元素和Web组件 (How to create Angular 6 C ...

  4. angular 模块构建_如何使用Angular和服务人员构建无需Internet即可运行的网站

    angular 模块构建 by Tomiwa 通过Tomiwa 如何使用Angular和服务人员构建无需Internet即可运行的网站 (How to build websites that work ...

  5. 4 angular 重构 项目_再遇angular(angular4项目实战指南)

    这两天看了看angular4的文档,发现他和angular1.X的差别真的是太大了,官方给出的那个管理英雄的Demo是一个非常好的入门项目,这里给出一个管理个人计划的小项目,从头至尾一步一步讲解如何去 ...

  6. angular 图片引入_推荐一个 angular 图像加载插件

    推荐一个简单的 Angular 图片加载插件:vgSrc,插件根据图片资源的不同加载状态,显示不同图片,亲测兼容IE-8. 使用 推荐使用 bower 加载: bower install vgSrc ...

  7. electron快捷键_如何在Electron JS应用程序中添加键盘快捷键?

    electron快捷键 Just like in any other native desktop application, keyboard shortcuts save time and make ...

  8. 【Spring】Spring 依赖注入之手动注入

    文章目录 1.概述 2.依赖回顾 2.依赖对象的初始化方式 2.1 通过构造器设置依赖对象 2.2 通过set方法设置依赖对象 3.spring依赖注入 3.1 通过构造器注入 3.1.1 根据构造器 ...

  9. mockito手动注入依赖_依赖注入–手动方式

    mockito手动注入依赖 依赖注入是一种将行为与依赖解决方案分开的技术. 用简单的话来说,它使开发人员可以定义具有特定功能的类,这些功能取决于各种协作者,而不必定义如何获取对这些协作者的引用. 以此 ...

最新文章

  1. 背景建模--Vibe 算法优缺点分析
  2. android组合动画还原,Android - Fragment,View动画,组合动画,属性动画
  3. 前端ui框架_跨屏建站发布同名响应式前端ui框架
  4. 前端学习(1296):第三方模块nodenrm
  5. CORS跨域漏洞的学习(防止CSRF漏洞导致的漏洞)
  6. 26期20180606 chmod chown umask 隐藏权限
  7. 二倍图三倍图什么意思_为什么说我们需要软件架构图?
  8. 【Flink】Flink Association with remote system akka Connection refused
  9. 经典的同态滤波算法的优化及其应用参数配置
  10. 基于SSM的大学生就业信息管理系统
  11. 超频到3200最佳时序_ddr4内存时序多少为好
  12. 【每日AI】什么是维数?
  13. 小型机和PC服务器差异分析
  14. open函数里的newline
  15. 如何用Java读取单元格的数据_Java读取Excel中的单元格数据
  16. 小a与星际探索(dp)
  17. 算法之美——循环移位(XY→YX)逆操作
  18. Altium Designer(四)排针类元件模型的创建
  19. 详解人工智能领域重大突破:GPT-3
  20. 最简单日柱推算法_乒乓球技术中的反手台内拧,如何练最简单?满场飞的乒乓球步法怎么练?反手拧拉两段练习法,一练就会 胖子说乒乓视频教学...

热门文章

  1. 【原创】告别恼人的水平滚动条——滚动条宽度到底是多少?
  2. [转载] Python中为什么len不是普通方法
  3. [转载] python里字典的用法_python中字典(Dictionary)用法实例详解
  4. 高速收发器之8B/10B编码
  5. Scrapy中的Spider
  6. full stack front end
  7. 捷信达会员管理系统SQL语句相关
  8. TCP报文段的首部格式
  9. Servlet/JSP-02 Servlet相关类
  10. [家里蹲大学数学杂志]第410期定积分难题