当前位置: 首页 > 编程笔记 >

AngularJS模块详解及示例代码

华永新
2023-03-14
本文向大家介绍AngularJS模块详解及示例代码,包括了AngularJS模块详解及示例代码的使用技巧和注意事项,需要的朋友参考一下

AngularJS支持模块化的方法。模块用于单独的逻辑表示服务,控制器,应用程序等,并保持代码的整洁。我们在单独的js文件中定义的模块,并将其命名为按照module.js文件形式。在这个例子中,我们要创建两个模块。

Application Module - 用于初始化控制器应用程序

Controller Module - 用于定义控制器

应用模块

mainApp.js

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

在这里,我们已经声明使用 angular.module 功能的应用程序 mainApp 模块。我们已经通过了一个空数组给它。此数组通常包含从属模块。

控制器模块

mainApp.controller("studentController", function($scope) {
  $scope.student = {
   firstName: "Mahesh",
   lastName: "Parashar",
   fees:500,
   subjects:[
     {name:'Physics',marks:70},
     {name:'Chemistry',marks:80},
     {name:'Math',marks:65},
     {name:'English',marks:75},
     {name:'Hindi',marks:67}
   ],
   fullName: function() {
     var studentObject;
     studentObject = $scope.student;
     return studentObject.firstName + " " + studentObject.lastName;
   }
  };
});

在这里,我们已经声明采用studentController模块的mainApp.controller功能的控制器。

使用模块

<div ng-app="mainApp" ng-controller="studentController">
..
<script src="mainApp.js"></script>
<script src="studentController.js"></script>

在这里,我们使用 ng-app 指令和控制器采用ng-controller指令应用模块。我们已经在主要的HTML页面导入mainApp.js和studentController.js。

示例

下面的例子将展示上述所有模块。

testAngularJS.htm

<html>
  <head>
	<title>Angular JS Modules</title>
	<style>
	table, th , td {
	 border: 1px solid grey;
	 border-collapse: collapse;
	 padding: 5px;
	}
	table tr:nth-child(odd) {
	 background-color: #f2f2f2;
	}
	table tr:nth-child(even) {
	 background-color: #ffffff;
	}
	</style>
	</head>
	<body>
	<h2>AngularJS Sample Application</h2>
	<div ng-app="mainApp" ng-controller="studentController">
	<table border="0">
	<tr><td>Enter first name:</td><td><input type="text" ng-model="student.firstName"></td></tr>
	<tr><td>Enter last name: </td><td><input type="text" ng-model="student.lastName"></td></tr>
	<tr><td>Name: </td><td>{{student.fullName()}}</td></tr>
	<tr><td>Subject:</td><td>
	<table>
	  <tr>
	   <th>Name</th>
	   <th>Marks</th>
	  </tr>
	  <tr ng-repeat="subject in student.subjects">
	   <td>{{ subject.name }}</td>
	   <td>{{ subject.marks }}</td>
	  </tr>
	</table>
	</td></tr>
	</table>
	</div>
	<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
	<script src="mainApp.js"></script>
	<script src="studentController.js"></script>
</body>
</html>

mainApp.js

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

studentController.js

mainApp.controller("studentController", function($scope) {
  $scope.student = {
   firstName: "Mahesh",
   lastName: "Parashar",
   fees:500,
   subjects:[
     {name:'Physics',marks:70},
     {name:'Chemistry',marks:80},
     {name:'Math',marks:65},
     {name:'English',marks:75},
     {name:'Hindi',marks:67}
   ],
   fullName: function() {
     var studentObject;
     studentObject = $scope.student;
     return studentObject.firstName + " " + studentObject.lastName;
   }
  };
});

输出

在Web浏览器打开textAngularJS.htm。看到结果如下。

以上就是AngularJS模块相关知识的资料整理,后续继续补充相关知识,谢谢大家对本站的支持!

 类似资料:
  • 本文向大家介绍AngularJS 模块化详解及实例代码,包括了AngularJS 模块化详解及实例代码的使用技巧和注意事项,需要的朋友参考一下 AngularJS有几大特性,比如:   1 MVC   2 模块化   3 指令系统   4 双向数据绑定 那么本篇就来看看AngularJS的模块化。   首先先说一下为什么要实现模块化:   1 增加了模块的可重用性   2 通过定义模块,实现加载顺

  • 本文向大家介绍AngularJS Ajax详解及示例代码,包括了AngularJS Ajax详解及示例代码的使用技巧和注意事项,需要的朋友参考一下 AngularJS提供$http控制,可以作为一项服务从服务器读取数据。服务器可以使一个数据库调用来获取记录。 AngularJS需要JSON格式的数据。一旦数据准备好,$http可以用以下面的方式从服务器得到数据。 在这里,data.txt中包含的学

  • 本文向大家介绍AngularJs Scope详解及示例代码,包括了AngularJs Scope详解及示例代码的使用技巧和注意事项,需要的朋友参考一下 一、什么是Scope?   scope(http://code.angularjs.org/1.0.2/docs/api/ng.$rootScope.Scope)是一个指向应用model的object。它也是expression(http://ww

  • 本文向大家介绍AngularJs Modules详解及示例代码,包括了AngularJs Modules详解及示例代码的使用技巧和注意事项,需要的朋友参考一下 一、什么是Module?   很多应用都有一个用于初始化、加载(wires是这个意思吗?)和启动应用的main方法。angular应用不需要main方法,作为替代,module提供有指定目的的声明式,描述应用如何启动。这样做有几项优点: 这

  • 本文向大家介绍AngularJS指令详解及示例代码,包括了AngularJS指令详解及示例代码的使用技巧和注意事项,需要的朋友参考一下 AngularJS指令用于扩展HTML。这些都是先从ng- 前缀的特殊属性。我们将讨论以下指令: ng-app - 该指令启动一个AngularJS应用。 ng-init - 该指令初始化应用程序数据。 ng-model - 此指令定义的模型,该模型是变量在Ang

  • 本文向大家介绍AngularJS表单详解及示例代码,包括了AngularJS表单详解及示例代码的使用技巧和注意事项,需要的朋友参考一下 AngularJS提供丰富填写表单和验证。我们可以用ng-click来处理AngularJS点击按钮事件,然后使用 $dirty 和 $invalid标志做验证的方式。使用novalidate表单声明禁止任何浏览器特定的验证。表单控件使用了大量的角活动。让我们快速