AngularJS 包含


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


在 HTML 中包含 HTML 文件

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


服务端包含

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

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

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

客户端包含

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

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


AngularJS 包含

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

  1. <body>
  2. <div class="container">
  3. <div ng-include="'myUsers_List.html'"></div>
  4. <div ng-include="'myUsers_Form.html'"></div>
  5. </div>
  6. </body>
 

步骤如下:


步骤 1: 创建 HTML 列表

myUsers_List.html

  1. <h3>Users</h3>
  2. <table class="table table-striped">
  3. <thead><tr>
  4. <th>Edit</th>
  5. <th>FirstName</th>
  6. <th>LastName</th>
  7. </tr></thead>
  8. <tbody><tr ng-repeat="user in users">
  9. <td>
  10. <button class="btn" ng-click="editUser(user.id)">
  11. <span class="glyphicon glyphicon-pencil"></span>&nbsp;&nbsp;Edit
  12. </button>
  13. </td>
  14. <td>{{ user.fName }}</td>
  15. <td>{{ user.lName }}</td>
  16. </tr></tbody>
  17. </table>

步骤 2: 创建 HTML 表单

myUsers_Form.html

  1. <button class="btn btn-success" ng-click="editUser('new')">
  2. <span class="glyphicon glyphicon-user"></span>CreateNewUser
  3. </button>
  4. <hr>
  5. <h3 ng-show="edit">CreateNewUser:</h3>
  6. <h3 ng-hide="edit">EditUser:</h3>
  7. <form class="form-horizontal">
  8. <div class="form-group">
  9. <label class="col-sm-2 control-label">FirstName:</label>
  10. <div class="col-sm-10">
  11. <input type="text" ng-model="fName" ng-disabled="!edit" placeholder="First Name">
  12. </div>
  13. </div>
  14. <div class="form-group">
  15. <label class="col-sm-2 control-label">LastName:</label>
  16. <div class="col-sm-10">
  17. <input type="text" ng-model="lName" ng-disabled="!edit" placeholder="Last Name">
  18. </div>
  19. </div>
  20. <div class="form-group">
  21. <label class="col-sm-2 control-label">Password:</label>
  22. <div class="col-sm-10">
  23. <input type="password" ng-model="passw1" placeholder="Password">
  24. </div>
  25. </div>
  26. <div class="form-group">
  27. <label class="col-sm-2 control-label">Repeat:</label>
  28. <div class="col-sm-10">
  29. <input type="password" ng-model="passw2" placeholder="Repeat Password">
  30. </div>
  31. </div>
  32. </form>
  33. <hr>
  34. <button class="btn btn-success" ng-disabled="error || incomplete">
  35. <span class="glyphicon glyphicon-save"></span>SaveChanges
  36. </button>

步骤 3: 创建控制器

myUsers.js

  1. angular.module('myApp',[]).controller('userCtrl',function($scope){
  2. $scope.fName ='';
  3. $scope.lName ='';
  4. $scope.passw1 ='';
  5. $scope.passw2 ='';
  6. $scope.users =[
  7. {id:1, fName:'Hege',lName:"Pege"},
  8. {id:2, fName:'Kim',lName:"Pim"},
  9. {id:3, fName:'Sal',lName:"Smith"},
  10. {id:4, fName:'Jack',lName:"Jones"},
  11. {id:5, fName:'John',lName:"Doe"},
  12. {id:6, fName:'Peter',lName:"Pan"}
  13. ];
  14. $scope.edit =true;
  15. $scope.error =false;
  16. $scope.incomplete =false;
  17. $scope.editUser =function(id){
  18. if(id =='new'){
  19. $scope.edit =true;
  20. $scope.incomplete =true;
  21. $scope.fName ='';
  22. $scope.lName ='';
  23. }else{
  24. $scope.edit =false;
  25. $scope.fName = $scope.users[id-1].fName;
  26. $scope.lName = $scope.users[id-1].lName;
  27. }
  28. };
  29. $scope.$watch('passw1',function(){$scope.test();});
  30. $scope.$watch('passw2',function(){$scope.test();});
  31. $scope.$watch('fName',function(){$scope.test();});
  32. $scope.$watch('lName',function(){$scope.test();});
  33. $scope.test =function(){
  34. if($scope.passw1 !== $scope.passw2){
  35. $scope.error =true;
  36. }else{
  37. $scope.error =false;
  38. }
  39. $scope.incomplete =false;
  40. if($scope.edit &&(!$scope.fName.length ||
  41. !$scope.lName.length ||
  42. !$scope.passw1.length ||!$scope.passw2.length)){
  43. $scope.incomplete =true;
  44. }
  45. };
  46. })
 

步骤 4: 创建主页

myUsers.html

 
  1. <!DOCTYPE html>
  2. <html>
  3. <link rel="stylesheet" href ="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  4. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  5. <body ng-app="myApp" ng-controller="userCtrl">
  6. <div class="container">
  7. <div ng-include="'myUsers_List.html'"></div>
  8. <div ng-include="'myUsers_Form.html'"></div>
  9. </div>
  10. <script src="myUsers.js"></script>
  11. </body>
  12. </html>
结果:
 

【18】AngularJS 包含的更多相关文章

  1. AngularJS 包含

    在 AngularJS 中,你可以在 HTML 中包含 HTML 文件. 在 HTML 中,目前还不支持包含 HTML 文件的功能. 大多服务端脚本都支持包含文件功能 (SSI: Server Sid ...

  2. AngularJS包含

    1.在HTML中包含HTML文件:在HTML中,目前还不支持包含HTML文件的功能: 2.服务端包含:大多数服务端脚本都支持文件功能(SSI),使用SSI,你可以在HTML中包含HTML文件,并发送到 ...

  3. 18.angularJS服务

    转自:https://www.cnblogs.com/best/tag/Angular/ 服务 AngularJS功能最基本的组件之一是服务(Service).服务为你的应用提供基于任务的功能.服务可 ...

  4. AngularJS 包含HTML文件

    类似于python tornado的include方法,同样是可以在一个html文件中加载另外一个html文件,这样可以不用重复的写一些几乎不改变的代码. 首先创建两个文件,然后代码如下: <! ...

  5. 夺命雷公狗—angularjs—18—angularjs的事件

    对于一款前端框架,提起事件,很容易让人联想到DOM事件,比如说鼠标点击以及页面滚动等.但是我们这里说的angular中的事件和DOM事件并不是一个东西. 事件的发布 我们可以通过 $emit() 以及 ...

  6. AngularJS(13)-包含

    AngularJS 包含 使用 AngularJS, 你可以使用 ng-include 指令来包含 HTML 内容: 实例 <body> <div class="conta ...

  7. AngularJS:包含

    ylbtech-AngularJS:包含 1.返回顶部 1. AngularJS 包含 在 AngularJS 中,你可以在 HTML 中包含 HTML 文件. 在 HTML 中包含 HTML 文件 ...

  8. 2.1:你的第一个AngularJS App

    本章,带你体验一个简单的开发流程,将一个静态的使用模拟数据的应用,变成具有AngularJS特性的动态web应用.在6-8章,作者将展示如何创建一个更复杂,更真实的AngularJS应用. 1.准备项 ...

  9. AngularJs之九(ending......)

    今天继续angularJs,但也是最后一篇关于它的了,基础部分差不多也就这些,后续有机会再写它的提升部分. 今天要写的也是一个基础的选择列表: 一:使用ng-options,数组进行循环. <d ...

随机推荐

  1. 27. FormPanel类的defaults属性

    defaults : Object defaults属性可以包含任意个name/value属性对,这些属性将会被添加到每一个元素中...例如, 为了自动向容器包含的每个Ext.Panel 元素的宽度添 ...

  2. FastDFS的介绍

    FastDFS的介绍 FastDFS FastDFS是由国人余庆所开发,其项目地址: https://github.com/happyfish100 FastDFS是一个轻量级的开源分布式文件系统,主 ...

  3. c++ gets函数

    函数名称:gets函数 函数结构:gets() 所需头文件:#include<cstdio> 函数作用:持续读入,直到遇到换行停止输出.

  4. RabbitMQ的一些基本操作

    $ sudo chkconfig rabbitmq-server on # 添加开机启动RabbitMQ服务 $ sudo /sbin/service rabbitmq-server start # ...

  5. 解决:阿里云ECS上启动tomcat后,第一次访问时间特别长

    Re在ECS上启动tomcat后,第一次访问时间特别长      2017-04-25 10:16:04 INFO com.world.socket.ServerSocketListener  25- ...

  6. request获取请求参数

    /** * 方式1 */ Enumeration<?> enu=request.getParameterNames(); while(enu.hasMoreElements()){ Str ...

  7. [ POI 2012 ] Letters

    \(\\\) \(Description\) 给出两个长度为 \(N\) 的字符串\(S_1,S_2\),且保证两个字符串中每一个字符出现次数相同. 现在一次操作可以交换相邻的两个字符,问将 \(S_ ...

  8. leetcode126 Word Ladder II

    思路: 宽搜过程中分层记录路径,递归还原.实现: class Solution { public: void getPath(string now, string beginWord, string ...

  9. Selenium基于Python web自动化基础二 -- 免登录、等待及unittest单元测试框架

    一.免登录在进行测试的过程中难免会遇到登录的情况,给测试工作添加了工作量,本文仅提供一些思路供参考解决方式:手动请求中添加cookies.火狐的profile文件记录信息实现.人工介入.万能验证码.去 ...

  10. HTTP、HTTP1.0、HTTP1.1、HTTP2.0——笔记

    笔记来源地址:https://mp.weixin.qq.com/s/T2IErLDxbWP1a-VbRkZZHg HTTP: HTTP是WWW数据通信的基础,是应用层协议. HTTP是干什么的?用来给 ...