Part 16 ng include directive in AngularJS

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

If the user selects List from the dropdownlist, the employee data should be presented using an unordered list

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>
时间: 2024-10-15 16:02:01

Part 16 ng include directive in AngularJS的相关文章

使用-MM生成include指令和依赖生成(make include directive and dependency generation with -MM)

I want a build rule to be triggered by an include directive if the target of the include is out of date or doesn't exist. Currently the makefile looks like this: program_NAME := wget++ program_H_SRCS := $(wildcard *.h) program_CXX_SRCS := $(wildcard

angular min js 118 Error ng areq http //errors angularjs

1.错误描述 angular.min.js:118 Error: [ng:areq] http://errors.angularjs.org/1.5.8/ng/areq?p0=username&p1=not%20a%20function%2C%20got%20undefined at Error (native) at http://127.0.0.1:8020/AngularJS/js/angular.min.js:6:412 at sb (http://127.0.0.1:8020/Angu

Part 15 AngularJS ng init directive

The ng-init directive allows you to evaluate an expression in the current scope. In the following example, the ng-init directive initializes employees variable which is then used in the ng-repeat directive to loop thru each employee. In a real world

Part 6 AngularJS ng repeat directive

ng-repeat is similar to foreach loop in C#. Let us understand this with an example. Here is what we want to do. 1. For each employee we have in the employees array we want a table row. With in each cell of the table row we to display employee Firstna

16个良好的 Bootstrap Angularjs 管理后台主题

1. Angle Bootstrap Admin App + AngularJS Preview | Details & Download 2. BeyondAdmin – Admin App + AngularJS + MVC Preview | Details & Download 3. Cube Bootstrap Admin Theme + AngularJS Preview | Details & Download 4. Inspina Responsive Admin

16.语句include和require的区别是什么?为避免多次包含同一文件,可用(?)语句代替它们?

require->require是无条件包含也就是如果一个流程里加入require,无论条件成立与否都会先执行 require include->include有返回值,而require没有(可能因为如此require的速度比include快) 注意:包含文件不存在或者语法错误的时候require是致命的,include不是 1.PHP程序执行到require()时,只会读取一次档案,故常放在程序开头,档案引入后PHP会将网页档重新编译,让引入档成为原先网页的一部分. 2.PHP程序执行到in

angular.min.js:118 Error: [ng:areq] http://errors.angularjs.org/1.5.8/ng/areq?

1,错误如图所示 简单说下错误原因是:没有js没有注册进去. 解决方法: 1.看下index.html有没有引入你的js文件. 2.看下app.js有没有注册js,比如我这次就是这步没做好,合并代码时冲掉了.如图 在后面加上丢失的invoidFolder.controllerrjs就好了.希望对别人有帮助 原文地址:https://www.cnblogs.com/qianyy/p/10485230.html

前端MVC学习总结——AngularJS验证、过滤器

前端MVC学习总结--AngularJS验证.过滤器 目录 一.验证 二.过滤器 2.1.内置过滤器 2.1.1.在模板中使用过滤器 2.1.2.在脚本中调用过滤函数 2.2.自定义过滤器 三.指令(directive) 3.1.支持AngularJS功能的指令 3.1.1.应用与模块(ng-app) 3.1.2.控制器(ng-Controller) 3.1.3.包含(ng-Include) 3.1.4.不绑定(ngNonBindable) 3.2.扩展表单元素的指令 3.2.1.ng-opti

angularJS学习资源最全汇总

基础 官方: http://docs.angularjs.org angularjs官方网站已被墙,可看 http://www.ngnice.com/: 官方zip下载包 https://github.com/dolymood/angular-packages,已增加docs服务,输入地址即可,例如:http://blog.aijc.net/angular-packages/angular-1.3.15/docs/ jquery?ag? : http://stackoverflow.com/qu