您的位置:首页 > Web前端 > AngularJS

AngularJS快速入门指南17:Includes

2015-05-19 06:15 441 查看
  使用AngularJS,你可以在HTML中包含其它的HTML文件。

在HTML中包含其它HTML文件?

  当前的HTML文档还不支持该功能。不过W3C建议在后续的HTML版本中增加HTML imports功能,以支持在HTML中包含其它的HTML文件。

<link rel="import" href="/path/navigation.html">


在服务端包含文件

  大部分的web服务器都支持服务端包含文件(Server Side Includes)。通过使用SSI,你可以在页面被发送到客户端浏览器之前将HTML文件包含到一段HTML文档中。例如下面的这行PHP代码:

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


在客户端包含文件

  通过JavaScript,我们可以有许多的方法将HTML文件加入到HTML文档中。

  最通用的做法莫过于使用Ajax,即通过异步http请求从服务端获取数据,然后动态将内容以innerHTML的形式输出到HTML元素中。

在AngularJS中包含文件

  在AngularJS中,你可以使用ng-include指令将HTML文件加入到HTML文档中:

<body>

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

</body>


  下面是完成上述页面的三个步骤。

第一步:创建myUsers_List.htm文件

<h3>Users</h3>

<table class="table table-striped">
<thead><tr>
<th>Edit</th>
<th>First Name</th>
<th>Last Name</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>


第二部:创建myUsers_Form.htm文件

<button class="btn btn-success" ng-click="editUser('new')">
<span class="glyphicon glyphicon-user"></span> Create New User
</button>
<hr>

<h3 ng-show="edit">Create New User:</h3>
<h3 ng-hide="edit">Edit User:</h3>

<form class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">First Name:</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">Last Name:</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> Save Changes
</button>


第三部:创建主页面文件

<!DOCTYPE html>
<html ng-app="">
<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-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快速入门指南16:Bootstrap
下一章 - AngularJS快速入门指南18:Application
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: