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. mysql db2免费下载_DB2ToMysql(DB2导入到Mysql工具)下载
  2. 1.1.linux命令.概要学习
  3. 【引用】phpmyadmin提示Access denied for user 'root'@'localhost' (using password: NO)的解决办法...
  4. 服务降级-多版本支持
  5. IDEA Docker 插件安装
  6. MySQL的sql优化
  7. 猿编程python,python编程猿
  8. 2003卡在应用计算机设置,计算机操作与应用(Windows XP+Office 2003)
  9. 贪心科技机器学习训练营(二)
  10. ChineseBERT: Chinese Pretraining Enhanced by Glyph and Pinyin Information
  11. Windows 10 驱动开发 及 驱动签名
  12. Backtrader量化平台教程(六)Analyzer
  13. 前端框架 — Bootstrap
  14. Python一对一题目辅导「PTA 题目讲解·难度系数:基础」
  15. 康考迪亚大学应用计算机科学,2020年康考迪亚大学IT专业会学什么内容
  16. crmeb多商户2.0正式版 新增DIY、PC端客服、同城配送平台等完整包下载更新包下载
  17. 理想电流源与理想电压源
  18. findx2鸿蒙系统,OPPO Find X2 Pro 官方降价 1000 华为全新系统来了
  19. Cache Tiering
  20. 教学管理系统的设计与实现(一)(源码,以及毕业论文)

热门文章

  1. 【Go】优雅的读取http请求或响应的数据-续
  2. 避免使用PHP保留字作为常量、类名和方法名,以及命名空间的命名
  3. Uvalive - 3026 Period (kmp求字符串的最小循环节+最大重复次数)
  4. POI导入数据的过程中,遇到读取以科学计数法显示的数据
  5. (转)Locale 详解
  6. Navicat15连接Oracal失败解决办法
  7. 数据结构上机实践第14周项目2 - 二叉树排序树中查找的路径
  8. linux文件内核目录,Linux内核目录文件简介
  9. python函数参数值_python 函数参数
  10. 树莓派模拟电路_基于树莓派的热电偶测量模块 MCC 134