Part 16 ng include directive in AngularJS

来源:http://www.cnblogs.com/gester/archive/2016/04/24/5426150.html
-Advertisement-
Play Games

ng-include directive is used to embed an HTML page into another HTML page. This technique is extremely useful when you want to reuse a specific view i ...


ng-include directive is used to embed an HTML page into another HTML page. This technique is extremely useful when you want to reuse a specific view in multiple pages in your application. 

The value of ng-include directive can be the name of the HTML page that you want to reuse or a property on the $scope object that points to the reusable HTML page.

EmployeeList.html : This is the HTML page that we intend to reuse on multiple HTML pages

<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Gender</th>
            <th>Salary</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="employee in employees">
            <td> {{ employee.name }} </td>
            <td> {{ employee.gender}} </td>
            <td> {{ employee.salary}} </td>
        </tr>
    </tbody>
</table>

Script.js : 

var app = angular
        .module("myModule", [])
        .controller("myController", function ($scope) { 
            var employees = [
                { name: "Ben", gender: "Male", salary: 55000 },
                { name: "Sara", gender: "Female", salary: 68000 },
                { name: "Mark", gender: "Male", salary: 57000 },
                { name: "Pam", gender: "Female", salary: 53000 },
                { name: "Todd", gender: "Male", salary: 60000 }
            ];
            $scope.employees = employees;
        });

HTMLPage1.html : This is the HTML page where we want to reuse EmployeeList.html. Notice that we are using ng-include directive and the value for it is the name of the HTML file that we want to reuse.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/angular.js"></script>
    <script src="Scripts/Script.js"></script>
    <link href="Styles.css" rel="stylesheet" />
</head>
<body ng-app="myModule">
    <div ng-controller="myController">
        <div ng-include="'EmployeeList.html'">
        </div>
    </div>
</body>
</html>

In this example, we have specified the name of the HTML file in the view. You can also have a property attached to the $scope object that points to the HTML file that you want to reuse , and use that property with ng-include directive. Here are the changes required to use a model property with ng-include directive.

Script.js : Notice, in the controller function we have employeeList property attached to the $scope object. This property points to the EmployeeList.html file that we want to reuse.

var app = angular
        .module("myModule", [])
        .controller("myController", function ($scope) {
 
            var employees = [
                { name: "Ben", gender: "Male", salary: 55000 },
                { name: "Sara", gender: "Female", salary: 68000 },
                { name: "Mark", gender: "Male", salary: 57000 },
                { name: "Pam", gender: "Female", salary: 53000 },
                { name: "Todd", gender: "Male", salary: 60000 }
            ];
 
            $scope.employees = employees;
            $scope.employeeList = "EmployeeList.html";
        });

HTMLPage1.html : Set the property employeeList that you have attached to the $scope object, as the value for ng-include directive

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/angular.js"></script>
    <script src="Scripts/Script.js"></script>
    <link href="Styles.css" rel="stylesheet" />
</head>
<body ng-app="myModule">
    <div ng-controller="myController">
        <div ng-include="employeeList"></div>
    </div>
</body>
</html>

Example : Create an HTML page with a dropdownlist that allows the user to select the view - Table or List. Depending on the selection we want to load the respective HTML page into the current HTML page i.e HTMLPage1.html

If the user selects Table from the dropdownlist, the employee data should be presented using a Table
ng-include example angularjs
If the user selects List from the dropdownlist, the employee data should be presented using an unordered list
angularjs ng include example
EmployeeTable.html : This HTML page presents the employee data using a table element

<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Gender</th>
            <th>Salary</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="employee in employees">
            <td> {{ employee.name }} </td>
            <td> {{ employee.gender}} </td>
            <td> {{ employee.salary}} </td>
        </tr>
    </tbody>
</table>

EmployeeList.html : This HTML page presents the employee data using 2 unordered list elements

<ul ng-repeat="employee in employees">
    <li>
        {{employee.name}}
        <ul>
            <li>{{employee.gender}}</li>
            <li>{{employee.salary}}</li>
        </ul>
    </li>
</ul>

Script.js : The controller function attaches employeeView property to the $scope object and sets it to EmployeeTable.html. This means when the page is initially loaded the employee data will be presented using a table.

var app = angular
        .module("myModule", [])
        .controller("myController", function ($scope) {
 
            var employees = [
                { name: "Ben", gender: "Male", salary: 55000 },
                { name: "Sara", gender: "Female", salary: 68000 },
                { name: "Mark", gender: "Male", salary: 57000 },
                { name: "Pam", gender: "Female", salary: 53000 },
                { name: "Todd", gender: "Male", salary: 60000 }
            ];
 
            $scope.employees = employees;
            $scope.employeeView = "EmployeeTable.html";
        });

HTMLPage1.html : This HTML page loads either the EmployeeTable.html or EmployeeList.html page depending on the item the user has selected from the dropdownlist.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/angular.js"></script>
    <script src="Scripts/Script.js"></script>
    <link href="Styles.css" rel="stylesheet" />
</head>
<body ng-app="myModule">
    <div ng-controller="myController">
        Select View :
        <select ng-model="employeeView">
            <option value="EmployeeTable.html">Table</option>
            <option value="EmployeeList.html">List</option>
        </select>
        <br /><br />
        <div ng-include="employeeView">
        </div>
    </div>
</body>
</html>

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 學習要點: 1.路徑組件 2.分頁組件 3.標簽組件 4.徽章組件 主講教師:李炎恢 本節課我們主要學習一下 Bootstrap 的四個組件功能:路徑組件、分頁組件、標簽組件和徽章組件。 一.路徑組件 路徑組件也叫做麵包屑導航。 //麵包屑導航 二.分頁組件 分頁組件可以提供帶有展示頁面的功能。 / ...
  • 學習要點: 1.輸入框組件 2.導航組件 3.導航條組件 主講教師:李炎恢 本節課我們主要學習一下Bootstrap的兩個個組件功能: 輸入框組件和導航導航條組件。 一.輸入框組件 文本輸入框就是可以在<input>元素前後加上文字或按鈕,可以實現對錶單控制項的擴展。 //在左側添加文字 //在右側添 ...
  • 學習要點: 1.小圖標組件 2.下拉菜單組件 3.按鈕組組件 4.按鈕式下拉菜單 主講教師:李炎恢 本節課我們主要學習一下 Bootstrap 的三個組件功能:小圖標組件、下拉菜單組件和各種按鈕組件。 一.小圖標組件 Bootstrap 提供了免費的 263 個小圖標(數了兩次),具體可以參考中文官 ...
  • 學習要點: 1.輔組類 2.響應式工具 主講教師:李炎恢 本節課我們主要學習一下 Bootstrap 的輔組類和響應式工具, 輔助類提供了一組類來輔組頁面設計,而響應式工具則利用媒體查詢顯示或隱藏某些內容。 一.輔助類 Bootstrap 在佈局方面提供了一些細小的輔組樣式,用於文字顏色以及背景色的 ...
  • 學習要點: 1.移動設備優先 2.佈局容器 3.柵格系統 主講教師:李炎恢 本節課我們主要學習一下 Bootstrap 的柵格系統,提供了一套響應式、移動設備優先的流式柵格系統。 一.移動設備優先 在 HTML5 的項目中,我們做了移動端的項目。它有一份非常重要的 meta,用於設置屏幕和設備等寬以 ...
  • In Angular there are several built in services. $http service is one of them. In this video, we will discuss another built in service, $log. It is als ...
  • Here is what we want to do1. Create an ASP.NET Web service. This web service retrieves the data from SQL Server database table, returns it in JSON for ...
  • 學習要點: 1.表單 2.圖片 主講教師:李炎恢 本節課我們主要學習一下 Bootstrap 表單和圖片功能,通過內置的 CSS 定義,顯示各種豐富的效果。 一.表單 Bootstrap 提供了一些豐富的表單樣式供開發者使用。 1.基本格式 //實現基本的表單樣式 註:只有正確設置了輸入框的 typ ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...