AngularJS中的包含详细介绍及实现示例


Posted in Javascript onJuly 28, 2016

AngularJS 包含

在 AngularJS 中,你可以在 HTML 中包含 HTML 文件。

在 HTML 中包含 HTML 文件

在 HTML 中,目前还不支持包含 HTML 文件的功能。

服务端包含

大多服务端脚本都支持包含文件功能 (SSI: Server Side Includes)。

使用 SSI, 你可在 HTML 中包含 HTML 文件,并发送到客户端浏览器。

PHP 实例

<?php require("navigation.php"); ?>

客户端包含

通过 JavaScript 有很多种方式可以在 HTML 中包含 HTML 文件。

通常我们使用 http 请求 (AJAX) 从服务端获取数据,返回的数据我们可以通过 使用 innerHTML 写入到 HTML 元素中。

AngularJS 包含

使用 AngularJS, 你可以使用 ng-include 指令来包含 HTML 内容:

实例

<body>

<div class="container">
 <div ng-include="'myUsers_List.htm'"></div>
 <div ng-include="'myUsers_Form.htm'"></div>
</div>

步骤如下:

步骤 1: 创建 HTML 列表

myUsers_List.html

<h1>用户</h1>

<table class="table table-striped">
 <thead><tr>
 <th>编辑</th>
 <th>名</th>
 <th>姓</th>
 </tr></thead>
 <tbody><tr ng-repeat="user in users">
 <td>
  <button class="btn" ng-click="editUser(user.id)">
  <span class="glyphicon glyphicon-pencil"></span>  Edit
  </button>
 </td>
 <td>{{ user.fName }}</td>
 <td>{{ user.lName }}</td>
 </tr></tbody>
</table>

运行结果:

用户

编辑
  Edit {{ user.fName }} {{ user.lName }}

步骤 2: 创建 HTML 表单

myUsers_Form.html

<button class="btn btn-success" ng-click="editUser('new')">
<span class="glyphicon glyphicon-user"></span>创建新用户
</button>
<hr>

<h3 ng-show="edit">创建新用户:</h3>
<h3 ng-hide="edit">编辑用户:</h3>

<form class="form-horizontal">
 <div class="form-group">
 <label class="col-sm-2 control-label">名:</label>
 <div class="col-sm-10">
 <input type="text" ng-model="fName" ng-disabled="!edit" placeholder="名">
 </div>
 </div> 
 <div class="form-group">
 <label class="col-sm-2 control-label">姓:</label>
 <div class="col-sm-10">
 <input type="text" ng-model="lName" ng-disabled="!edit" placeholder="姓">
 </div>
 </div>
 <div class="form-group">
 <label class="col-sm-2 control-label">密码:</label>
 <div class="col-sm-10">
 <input type="password" ng-model="passw1" placeholder="密码">
 </div>
 </div>
 <div class="form-group">
 <label class="col-sm-2 control-label">重复密码:</label>
 <div class="col-sm-10">
 <input type="password" ng-model="passw2" placeholder="重复密码">
 </div>
 </div>
</form>

<hr>
<button class="btn btn-success" ng-disabled="error || incomplete">
<span class="glyphicon glyphicon-save"></span>保存
</button>

运行结果:

AngularJS中的包含详细介绍及实现示例

步骤 3: 创建控制器

myUsers.js

angular.module('myApp', []).controller('userCtrl', function($scope) {
$scope.fName = '';
$scope.lName = '';
$scope.passw1 = '';
$scope.passw2 = '';
$scope.users = [
{id:1, fName:'Hege',lName:"Pege" },
{id:2, fName:'Kim',lName:"Pim" },
{id:3, fName:'Sal',lName:"Smith" },
{id:4, fName:'Jack',lName:"Jones" },
{id:5, fName:'John',lName:"Doe" },
{id:6, fName:'Peter',lName:"Pan" }
];
$scope.edit = true;
$scope.error = false;
$scope.incomplete = false; 
$scope.editUser = function(id) {
 if (id == 'new') {
 $scope.edit = true;
 $scope.incomplete = true;
 $scope.fName = '';
 $scope.lName = '';
 } else {
 $scope.edit = false;
 $scope.fName = $scope.users[id-1].fName;
 $scope.lName = $scope.users[id-1].lName; 
 }
};

$scope.$watch('passw1',function() {$scope.test();});
$scope.$watch('passw2',function() {$scope.test();});
$scope.$watch('fName',function() {$scope.test();});
$scope.$watch('lName',function() {$scope.test();});

$scope.test = function() {
 if ($scope.passw1 !== $scope.passw2) {
 $scope.error = true;
 } else {
 $scope.error = false;
 }
 $scope.incomplete = false;
 if ($scope.edit && (!$scope.fName.length ||
 !$scope.lName.length ||
 !$scope.passw1.length || !$scope.passw2.length)) {
 $scope.incomplete = true;
 }
};
})

步骤 4: 创建主页

myUsers.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="//apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="userCtrl">

<div class="container">
 <div ng-include="'myUsers_List.htm'"></div>
 <div ng-include="'myUsers_Form.htm'"></div>
</div>

<script src= "myUsers.js"></script>

</body>
</html>

运行结果:

 AngularJS中的包含详细介绍及实现示例

AngularJS中的包含详细介绍及实现示例

以上就是 对AngularJS 包含资料的整理,希望能帮助AngularJS 编程的朋友。

Javascript 相关文章推荐
JS:window.onload的使用介绍
Nov 13 Javascript
IE下双击checkbox反应延迟问题的解决方法
Mar 27 Javascript
JQuery.get提交页面不跳转的解决方法
Jan 13 Javascript
JavaScript下的时间格式处理函数Date.prototype.format
Jan 27 Javascript
实例讲解jQuery EasyUI tree中state属性慎用
Apr 01 Javascript
JS中对Cookie的操作详解
Aug 05 Javascript
AngularJs表单校验功能实例代码
Feb 09 Javascript
HTML5开发Kinect体感游戏的实例应用
Sep 18 Javascript
swiper 解决动态加载数据滑动失效的问题
Feb 26 Javascript
使用vuex缓存数据并优化自己的vuex-cache
May 30 Javascript
layui点击导航栏刷新tab页的示例代码
Aug 14 Javascript
layui动态绑定事件的方法
Sep 20 Javascript
AngularJS Bootstrap详细介绍及实例代码
Jul 28 #Javascript
JavaScript中windows.open()、windows.close()方法详解
Jul 28 #Javascript
JS中正则表达式只有3种匹配模式(没有单行模式)详解
Jul 28 #Javascript
AngularJS中的API(接口)简单实现
Jul 28 #Javascript
js 声明数组和向数组中添加对象变量的简单实例
Jul 28 #Javascript
AngularJS 输入验证详解及实例代码
Jul 28 #Javascript
jquery实用技巧之输入框提示语句
Jul 28 #Javascript
You might like
一台收音机,让一家人都笑逐颜开!
2020/08/21 无线电
一段php加密解密的代码
2006/10/09 PHP
PHP中的正规表达式(二)
2006/10/09 PHP
smarty实例教程
2006/11/19 PHP
php设计模式 Command(命令模式)
2011/06/26 PHP
php二维数组转成字符串示例
2014/02/17 PHP
PHP实现原比例生成缩略图的方法
2016/02/03 PHP
浅析Yii2 GridView 日期格式化并实现日期可搜索教程
2016/04/22 PHP
php 的反射详解及示例代码
2016/08/25 PHP
jQuery事件绑定.on()简要概述及应用
2013/02/07 Javascript
JavaScript四种调用模式和this示例介绍
2014/01/02 Javascript
js中的onchange和onpropertychange (onchange无效的解决方法)
2014/03/08 Javascript
extjs 如何给column 加上提示
2014/07/29 Javascript
jquery使用animate方法实现控制元素移动
2015/03/27 Javascript
CSS javascript 结合实现悬浮固定菜单效果
2015/08/23 Javascript
JS实现浏览器状态栏文字从右向左弹出效果代码
2015/10/27 Javascript
Jquery实现select multiple左右添加和删除功能的简单实例
2016/05/26 Javascript
JS获取url参数,JS发送json格式的POST请求方法
2018/03/29 Javascript
el-select数据过多懒加载的解决(loadmore)
2019/05/29 Javascript
vue 中的动态传参和query传参操作
2020/11/09 Javascript
[03:28]2014DOTA2国际邀请赛 EG战队官方纪录片
2014/07/21 DOTA
python开发的小球完全弹性碰撞游戏代码
2013/10/15 Python
Python内置函数Type()函数一个有趣的用法
2015/02/18 Python
Python实现Sqlite将字段当做索引进行查询的方法
2016/07/21 Python
Python基于回溯法子集树模板解决马踏棋盘问题示例
2017/09/11 Python
为什么Python中没有&quot;a++&quot;这种写法
2018/11/27 Python
Python tkinter界面实现历史天气查询的示例代码
2020/08/23 Python
python 如何区分return和yield
2020/09/22 Python
澳洲本土太阳镜品牌:Quay Australia
2019/07/29 全球购物
母亲节演讲稿范文
2014/01/02 职场文书
园林专业毕业生自荐信
2014/07/04 职场文书
群众路线领导班子整改方案
2014/10/25 职场文书
2014年人力资源部工作总结
2014/11/19 职场文书
幼儿园感恩节活动总结
2015/03/24 职场文书
运动员加油词
2015/07/18 职场文书
python实现简单聊天功能
2021/07/07 Python