PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > AngularJS教程 > 正文

    AngularJS 包含

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:AngularJS包含在 AngularJS 中,你可以在 HTML 中包含 HTML 文件。在 HTML 中包含 HTML 文件在 HTML 中,目前还不支持包含 HT...

    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.  
    3. <div class="container"> 
    4.   <div ng-include="'myUsers_List.htm'"></div> 
    5.   <div ng-include="'myUsers_Form.htm'"></div> 
    6. </div> 
    7.  
    8. </body> 

    步骤 1: 创建 HTML 列表
    myUsers_List.html


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

     

    步骤 2: 创建 HTML 表单

    myUsers_Form.html

    1. <button class="btn btn-success" ng-click="editUser('new')"> 
    2.   <span class="glyphicon glyphicon-user"></span> Create New User 
    3. </button> 
    4. <hr> 
    5.  
    6. <h3 ng-show="edit">Create New User:</h3> 
    7. <h3 ng-hide="edit">Edit User:</h3> 
    8.  
    9. <form class="form-horizontal"> 
    10. <div class="form-group"> 
    11.   <label class="col-sm-2 control-label">First Name:</label> 
    12.   <div class="col-sm-10"> 
    13.     <input type="text" ng-model="fName" ng-disabled="!edit" placeholder="First Name"> 
    14.   </div> 
    15. </div>  
    16. <div class="form-group"> 
    17.   <label class="col-sm-2 control-label">Last Name:</label> 
    18.   <div class="col-sm-10"> 
    19.     <input type="text" ng-model="lName" ng-disabled="!edit" placeholder="Last Name"> 
    20.   </div> 
    21. </div> 
    22. <div class="form-group"> 
    23.   <label class="col-sm-2 control-label">Password:</label> 
    24.   <div class="col-sm-10"> 
    25.     <input type="password" ng-model="passw1" placeholder="Password"> 
    26.   </div> 
    27. </div> 
    28. <div class="form-group"> 
    29.   <label class="col-sm-2 control-label">Repeat:</label> 
    30.   <div class="col-sm-10"> 
    31.     <input type="password" ng-model="passw2" placeholder="Repeat Password"> 
    32.   </div> 
    33. </div> 
    34. </form> 
    35.  
    36. <hr> 
    37. <button class="btn btn-success" ng-disabled="error || incomplete"> 
    38.   <span class="glyphicon glyphicon-save"></span> Save Changes 
    39. </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.  
    30. $scope.$watch('passw1',function() {$scope.test();}); 
    31. $scope.$watch('passw2',function() {$scope.test();}); 
    32. $scope.$watch('fName',function() {$scope.test();}); 
    33. $scope.$watch('lName',function() {$scope.test();}); 
    34.  
    35. $scope.test = function() { 
    36.   if ($scope.passw1 !== $scope.passw2) { 
    37.     $scope.error = true
    38.     } else { 
    39.     $scope.error = false
    40.   } 
    41.   $scope.incomplete = false
    42.   if ($scope.edit && (!$scope.fName.length || 
    43.     !$scope.lName.length || 
    44.     !$scope.passw1.length || !$scope.passw2.length)) { 
    45.     $scope.incomplete = true
    46.   } 
    47. }; 
    48. }) 

     

    步骤 4: 创建主页

    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.  
    6. <body ng-app="myApp" ng-controller="userCtrl"> 
    7.  
    8. <div class="container"> 
    9.   <div ng-include="'myUsers_List.htm'"></div> 
    10.   <div ng-include="'myUsers_Form.htm'"></div> 
    11. </div> 
    12.  
    13. <script src"myUsers.js"></script> 
    14.  
    15. </body> 
    16.  
    17. </html> 

     

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-152-3449-1.html
    相关热词搜索:
    下一篇:AngularJS 动画