博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
angular 手动注入_手动引导Angular JS应用程序
阅读量:2530 次
发布时间:2019-05-11

本文共 4092 字,大约阅读时间需要 13 分钟。

angular 手动注入

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
	        	
AngularJS Application

初始化过程 (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 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

			
AngularJS Bootstrap

{

{ message }}!

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.

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

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

翻译自:

angular 手动注入

转载地址:http://hfqzd.baihongyu.com/

你可能感兴趣的文章
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_43、SpringBoot2.x异步任务实战(核心知识)...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_01课程简介
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第11节 Logback日志框架介绍和SpringBoot整合实战_45、SpringBoot2.x日志讲解和Logback配置实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_02技术选型
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_汇总
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_01传统架构演进到分布式架构
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_02 微服务核心基础讲解
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_04微服务下电商项目基础模块设计...
查看>>