【18】AngularJS 包含
AngularJS 包含
在 AngularJS 中,你可以在 HTML 中包含 HTML 文件。
在 HTML 中包含 HTML 文件
在 HTML 中,目前还不支持包含 HTML 文件的功能。
服务端包含
大多服务端脚本都支持包含文件功能 (SSI: Server Side Includes)。
使用 SSI, 你可在 HTML 中包含 HTML 文件,并发送到客户端浏览器。
<?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.html'"></div>
<div ng-include="'myUsers_Form.html'"></div>
</div>
</body>
步骤如下:
步骤 1: 创建 HTML 列表
myUsers_List.html
<h3>Users</h3>
<table class="table table-striped">
<thead><tr>
<th>Edit</th>
<th>FirstName</th>
<th>LastName</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>
步骤 2: 创建 HTML 表单
myUsers_Form.html
<button class="btn btn-success" ng-click="editUser('new')">
<span class="glyphicon glyphicon-user"></span>CreateNewUser
</button>
<hr>
<h3 ng-show="edit">CreateNewUser:</h3>
<h3 ng-hide="edit">EditUser:</h3>
<form class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">FirstName:</label>
<div class="col-sm-10">
<input type="text" ng-model="fName" ng-disabled="!edit" placeholder="First Name">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">LastName:</label>
<div class="col-sm-10">
<input type="text" ng-model="lName" ng-disabled="!edit" placeholder="Last Name">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Password:</label>
<div class="col-sm-10">
<input type="password" ng-model="passw1" placeholder="Password">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Repeat:</label>
<div class="col-sm-10">
<input type="password" ng-model="passw2" placeholder="Repeat Password">
</div>
</div>
</form>
<hr>
<button class="btn btn-success" ng-disabled="error || incomplete">
<span class="glyphicon glyphicon-save"></span>SaveChanges
</button>
步骤 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>
<link rel="stylesheet" href ="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body ng-app="myApp" ng-controller="userCtrl">
<div class="container">
<div ng-include="'myUsers_List.html'"></div>
<div ng-include="'myUsers_Form.html'"></div>
</div>
<script src="myUsers.js"></script>
</body>
</html>

【18】AngularJS 包含的更多相关文章
- AngularJS 包含
在 AngularJS 中,你可以在 HTML 中包含 HTML 文件. 在 HTML 中,目前还不支持包含 HTML 文件的功能. 大多服务端脚本都支持包含文件功能 (SSI: Server Sid ...
- AngularJS包含
1.在HTML中包含HTML文件:在HTML中,目前还不支持包含HTML文件的功能: 2.服务端包含:大多数服务端脚本都支持文件功能(SSI),使用SSI,你可以在HTML中包含HTML文件,并发送到 ...
- 18.angularJS服务
转自:https://www.cnblogs.com/best/tag/Angular/ 服务 AngularJS功能最基本的组件之一是服务(Service).服务为你的应用提供基于任务的功能.服务可 ...
- AngularJS 包含HTML文件
类似于python tornado的include方法,同样是可以在一个html文件中加载另外一个html文件,这样可以不用重复的写一些几乎不改变的代码. 首先创建两个文件,然后代码如下: <! ...
- 夺命雷公狗—angularjs—18—angularjs的事件
对于一款前端框架,提起事件,很容易让人联想到DOM事件,比如说鼠标点击以及页面滚动等.但是我们这里说的angular中的事件和DOM事件并不是一个东西. 事件的发布 我们可以通过 $emit() 以及 ...
- AngularJS(13)-包含
AngularJS 包含 使用 AngularJS, 你可以使用 ng-include 指令来包含 HTML 内容: 实例 <body> <div class="conta ...
- AngularJS:包含
ylbtech-AngularJS:包含 1.返回顶部 1. AngularJS 包含 在 AngularJS 中,你可以在 HTML 中包含 HTML 文件. 在 HTML 中包含 HTML 文件 ...
- 2.1:你的第一个AngularJS App
本章,带你体验一个简单的开发流程,将一个静态的使用模拟数据的应用,变成具有AngularJS特性的动态web应用.在6-8章,作者将展示如何创建一个更复杂,更真实的AngularJS应用. 1.准备项 ...
- AngularJs之九(ending......)
今天继续angularJs,但也是最后一篇关于它的了,基础部分差不多也就这些,后续有机会再写它的提升部分. 今天要写的也是一个基础的选择列表: 一:使用ng-options,数组进行循环. <d ...
随机推荐
- Ubuntu搭建Eclipse+JDK+SDK的Android (转载)
转自:http://blog.csdn.net/ithomer/article/details/6960989 今晚重装Ubuntu系统,重新安装了一套eclipse+jdk+SDK的Android开 ...
- vue商品详情页添加动画(eg)
<template> <div class="food" transition="move"></div> </tem ...
- codeforces 402E - Strictly Positive Matrix【tarjan】
首先认识一下01邻接矩阵k次幂的意义:经过k条边(x,y)之间的路径条数 所以可以把矩阵当成邻接矩阵,全是>0的话意味着两两之间都能相连,也就是整个都要在一个强连通分量里,所以直接tarjan染 ...
- bzoj1216 操作系统(优先队列模拟)
1216: [HNOI2003]操作系统 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1172 Solved: 649[Submit][Statu ...
- Android项目模块化遇到的问题
1.问题背景 gradle 4 MacOs 10.14.3 Android Studio 3 在android模块化的时候,例如,有两个模块,一个是usercenter,另一个是common. 其中u ...
- 数学 FZU 2074 Number of methods
题目传送门 /* 数学:假设取了第i个,有C(n-1)(i-1)种取法 则ans = sum (C(n-1)(i-1)) (1<i<=n) 即2^(n-1) */ #include < ...
- 20 如何在C#中存一批数据,数组
使用软件的一个重要原因,是因为软件可以帮我们重复处理很多事情.在前面我们已经讲到了循环.循环就是为了重复处理一个事情.那么我们有没有想过,我们要重复处理的一批数据怎么在程序里存放呢? 举个例子吧. 我 ...
- Scala-基础-函数(2)
import junit.framework.TestCase //函数(2) //知识点-默认参数,带名参数,变长参数,过程 class Demo1 extends TestCase { //测试方 ...
- vue-devtools是vue浏览器调试工具
开vue官网在vue-生态系统-工具可以看到vue-devtools这个工具: vue-devtools是一款基于chrome游览器的插件,用于调试vue应用,这可以极大地提高我们的调试效率.接下来我 ...
- 学习笔记 第五章 使用CSS美化网页文本
第五章 使用CSS美化网页文本 学习重点 定义字体类型.大小.颜色等字体样式: 设计文本样式,如对齐.行高.间距等: 能够灵活设计美观.实用的网页正文版式. 5.1 字体样式 5.1.1 定义字体 ...