js模块封装示例

In this post, we are going to cover one of the most important feature in AngularJS called Modules. We looked at features like controllers, filters and directives in the previous posts. We have also discussed how to create and include controllers in angular application, but it is not a recommended way to do, especially when you are dealing with big applications. We can use AngularJS Module API to modularize angular applications.

在本文中,我们将介绍AngularJS中最重要的功能之一,称为Modules 。 我们在之前的文章中介绍了控制器 , 过滤器和指令等功能。 我们还讨论了如何在角度应用程序中创建和包含控制器,但是不建议这样做,特别是在处理大型应用程序时。 我们可以使用AngularJS Module API来模块化角度应用程序。

内容 (Contents)

  • What is a Module?什么是模块?
  • Creating a Controller in a Module在模块中创建控制器
  • Creating a Controller in a Module Example在模块示例中创建控制器
  • AngularJS Application FilesAngularJS应用程序文件

什么是模块? (What is a Module?)

Module can be treated as a container for the different parts of your application like controllers, filters, services, directives ,etc, which specify how an application should be bootstrapped. Module is an important part of the AngularJS dependency injection system.

可以将模块视为应用程序不同部分(如controllersfiltersservicesdirectives ,等)的容器,这些部分指定应如何引导应用程序。 模块是AngularJS 依赖注入系统的重要组成部分。

Here is the general syntax for creating a module:

这是创建模块的一般语法:

  • angular.module(“myModule”, []);angular.module(“ myModule”,[]);

Here myModule is the name of the module. [] is where you inject the dependencies.

这里myModule是模块的名称。 []是注入依赖项的地方。

Why Modules?

为什么选择模块?

  • Helps package our code into reusable modules.帮助将我们的代码打包到可重用的模块中。
  • The declarative process is easier to understand.声明过程更容易理解。
  • Modules can be loaded in any order.可以以任何顺序加载模块。
  • Easily testable and maintainable components.易于测试和维护的组件。
  • Helps organize your application.帮助组织您的应用程序。

在模块中创建控制器 (Creating a Controller in a Module)

In this section, we are going to explain how to create a controller in a module. It is very simple and we use the following steps to achieve that task.

在本节中,我们将解释如何在模块中创建控制器。 这非常简单,我们使用以下步骤来完成该任务。

var myFirstModule = angular.module("myFirstModule ", []);myFirstModule.controller("MyController", function);

Here we have created a module called myFirstModule and defined a controller named MyController in it.

在这里,我们创建了一个名为myFirstModule的模块,并在其中定义了一个名为MyController的控制器。

You can also create services, providers and factories using the angular module API. The following table briefly describes some of the mostly used methods in the API.

您还可以使用角度模块API创建服务,提供者和工厂。 下表简要介绍了API中一些最常用的方法。

Method Syntax
module.service module.service( ‘serviceName’, function );
module.factory module.factory( ‘factoryName’, function );
module.provider module.provider( ‘providerName’, function );
方法 句法
模块服务 module.service('serviceName',function);
模块工厂 module.factory('factoryName',function);
模块提供者 module.provider('providerName',function);

In this post, we are not going to look into the details of these concepts. We will see these concepts in the coming posts.

在本文中,我们将不研究这些概念的细节。 我们将在接下来的文章中看到这些概念。

在模块示例中创建控制器 (Creating a Controller in a Module Example)

The following example demonstrates how to create a controller in angular module. In this example, we will create a module called demoApp and will define the controller, blogController in it.

以下示例演示了如何在角度模块中创建控制器。 在此示例中,我们将创建一个名为demoApp的模块, 在其中定义控制器blogController

angular-module

angular-module

<!DOCTYPE html>
<html ng-app="demoApp"><head>
<title> AngularJS - Module</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head><body><div ng-controller="blogController">First Name: <input type="text" ng-model="firstName"><br>Last Name: <input type="text" ng-model="lastName"><br><br>Full Name: {{firstName + " " + lastName}}<br>Blog Name : {{blogName }}</div><script>var app = angular.module("demoApp", []);app.controller("blogController", function($scope) {$scope.firstName = "Pankaj",$scope.lastName = "Kumar",$scope.blogName = "JournalDev"
});</script></body>
</html>

We use ng-app directive to specify the name of the module used in the application. You will see the following output in your browser.

我们使用ng-app指令来指定应用程序中使用的模块的名称。 您将在浏览器中看到以下输出。

演示地址

AngularJS应用程序文件 (AngularJS Application Files)

Earlier we looked at how to include controllers in external files. That is very helpful when you deal with big applications. In any angular application, we use mainly two types of files, one with module and other with controllers. Let’s look in to it.
The following example demonstrates this usage.

之前,我们研究了如何在外部文件中包含控制器。 当您处理大型应用程序时,这非常有帮助。 在任何角度应用程序中,我们主要使用两种类型的文件,一种使用模块,另一种使用控制器。 让我们来看一下。
以下示例演示了此用法。

First, we create a module in the app.js file.

首先,我们在app.js文件中创建一个模块。

app.js

app.js

var app = angular.module("demoApp", []);

Second step is to define the controllers in controller.js file.

第二步是在controller.js文件中定义控制器。

controller.js

controller.js

app.controller("blogController", function($scope) {$scope.firstName = "Pankaj",$scope.lastName = "Kumar",$scope.blogName = "JournalDev"
});

Third step is to include these two files in our application.

第三步是在我们的应用程序中包括这两个文件。

angular-module

angular-module

<!DOCTYPE html>
<html ng-app="demoApp"><head>
<title> AngularJS - Module</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head><body><div ng-controller="blogController">First Name: <input type="text" ng-model="firstName"><br>Last Name: <input type="text" ng-model="lastName"><br><br>Full Name: {{firstName + " " + lastName}}<br>Blog Name : {{blogName }}</div><script src="app.js"></script>
<script src="controller.js"></script></body>
</html>

You will see the following output in your browser.

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

演示地址

That’s all for now and we will see more angular concepts in the coming posts.

到此为止,我们将在以后的文章中看到更多的角度概念。

翻译自: https://www.journaldev.com/6125/angularjs-module-tutorial-example

js模块封装示例

js模块封装示例_AngularJS模块教程示例相关推荐

  1. matlab中模块封装,MATLAB/simulink模块的封装

    编辑推荐: 本文来自于新浪博客,介绍的是关于模块的封装:搭建模型,设计参数,编辑参数框等. 今天重新回到MATLAB/simulink,介绍的是关于模块的封装的介绍. 首先搭建一个简单的模型: 全选, ...

  2. Magicodes.Pay,打造开箱即用的统一支付库,已提供ABP模块封装

    Magicodes.Pay,打造开箱即用的统一支付库,已提供ABP模块封装 简介 Magicodes.Pay,是心莱科技团队提供的统一支付库,相关库均使用.NET标准库编写,支持.NET Framew ...

  3. python共享内存通信mapofview_python map eval strPython socket模块实现的udp通信功能示例...

    本文实例讲述了Python socket模块实现的udp通信功能.分享给大家供大家参考,具体如下: socket介绍 socket(简称 套接字) 是进程间通信的一种方式,它与其他进程间通信的一个主要 ...

  4. Python 3.X 调用多线程C模块,并在C模块中回调python函数的示例

    由于最近在做一个C++面向Python的API封装项目,因此需要用到C扩展Python的相关知识.在此进行简要的总结. 此篇示例分为三部分.第一部分展示了如何用C在Windows中进行多线程编程:第二 ...

  5. boost::timer模块timer、job_timer和progress_display示例程序

    boost::timer模块timer.job_timer和progress_display示例程序 实现功能 C++实现代码 实现功能 boost::timer模块timer.job_timer和p ...

  6. boost::spirit模块演示 AST 生成的计算器示例,AST一旦创建就会被遍历

    boost::spirit模块演示 AST 生成的计算器示例,AST一旦创建就会被遍历 实现功能 C++实现代码 实现功能 boost::spirit模块演示 AST 生成的计算器示例,AST一旦创建 ...

  7. boost::sort模块实现spreadsort 字符串函子排序示例

    boost::sort模块实现spreadsort 字符串函子排序示例 实现功能 C++实现代码 实现功能 boost::sort模块实现spreadsort 字符串函子排序示例 C++实现代码 #i ...

  8. boost::sort模块实现带有右移函子排序示例的整数排序的测试程序

    boost::sort模块实现带有右移函子排序示例的整数排序的测试程序 实现功能 C++实现代码 实现功能 boost::sort模块实现带有右移函子排序示例的整数排序的测试程序 C++实现代码 #i ...

  9. boost::sort模块实现spreadsort 反向字符串排序示例

    boost::sort模块实现spreadsort 反向字符串排序示例 实现功能 C++实现代码 实现功能 boost::sort模块实现spreadsort 反向字符串排序示例 C++实现代码 #i ...

最新文章

  1. 【开源】一键生成各种姿势的火柴人gif:在线录制真人视频即可转换
  2. R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(自定义分组颜色、主题)实战(dot plot)
  3. delphi listview怎么自动宽度_数控弯字机怎么对刀呢?
  4. 【Flask】Jinja2之模板中使用url_for
  5. python 多分类模型优化_【Python与机器学习】:利用Keras进行多类分类
  6. 28335之SCI模块
  7. 记录android点滴(一)--通过build.prop实现产品定制的方法
  8. java udp简单聊天程序_Java基于UDP协议实现简单的聊天室程序
  9. jupyter安装与迁移文件
  10. 【连载】如何掌握openGauss数据库核心技术?秘诀三:拿捏存储技术(2)
  11. 中国太阳能热市场趋势报告、技术动态创新及市场预测
  12. 像excel一样规律填充(二)
  13. 西门子1200跟V90伺服总线通讯①_设置V90
  14. java框架常见面试题_java框架面试题总结
  15. 手把手教你使用si9000计算高速差分线的阻抗
  16. 一个小白程序员的目标
  17. 网关和路由器功能的有哪些不同
  18. [爬虫实战]利用python快速爬取NCBI中参考基因组assembly的相关信息
  19. 月薪过万必会的:双亲委托模型
  20. 微软服务器dda,Windows 10 版本2004 微软官方原版镜像

热门文章

  1. 删除数据表中的重复行
  2. SQL Server 2005 正则表达式使模式匹配和数据提取变得更容易
  3. 人生感悟:生活磨练有时也是一种财富
  4. C语言进阶——使用C语言与gnuplot结合画一些波形
  5. [转载] python enumerate函数 实例_python中使用enumerate函数遍历元素实例
  6. [转载] C++11初始化列表与参数列表的作用
  7. [转载] 第一个Python CGI编程和配置
  8. [转载] python 命名空间
  9. 两百个jQuery插件集合
  10. 兔子--html,js,php,ASP,ASP.NET,JSP的关系