AngularJs 的ng-include指令的使用
AngularJs通过指令ng-include来将页面中共用的模块分离出来,这个功能和mvc里面的分部页的作用一样的。
先看文件的结构:
父页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="https://cdn.staticfile.org/angular.js/1.7.5/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="ParentCtr">
<div ng-include="'Childrens/Header.html'"></div>
<input type="text" ng-model="test" />
<p style="background-color:darkgoldenrod">这里演示在一个HTML页面中,通过引用子页面来完成整个页面功能</p> <div ng-include="'Childrens/Footer.html'"></div>
<script type="text/javascript">
var formapp = angular.module('myApp', []);
formapp.controller('ParentCtr', function ($scope) {
$scope.test = "hello";
$scope.$on('to-parent', function (d, data) {
$scope.$broadcast('to-child', data);
});
})
.controller("headerCtr", function ($scope) {
$scope.headerTitle = "头部";
$scope.btnheader = function () {
$scope.headerTitle = "点击了按钮";
}
$scope.btnheader1 = function () {
$scope.$emit('to-parent', $scope.headerTitle);
}
})
.controller("footerCtr", function ($scope) {
$scope.ft = "尾部";
$scope.list = ["abc", "efg", "xyz"];
$scope.$on('to-child', function (d, data) {
$scope.fromHeader= data;
});
});
</script>
</body> </html>
子页面Header.html:
<div ng-controller="headerCtr">
<h2>这是头部</h2>
<input ng-model="headerTitle" />
<input type="button" ng-click="btnheader()" value="点击我" />
<input type="button" ng-click="btnheader1()" value="点击我,将文本框的内容显示到尾部" />
</div>
子页面Footer.html:
<div ng-controller="footerCtr">
<h2>这是尾部</h2>
<p>{{ft}}</p>
<p>{{fromHeader}}</p>
<ul>
<li ng-repeat="t in list">{{t}}</li>
</ul>
</div>
运行效果:
在不同的控制器之间传值比较麻烦,同级的控制器之间不能相互传值,而是通过父级控制器来传值。具体步骤:
当点击头部的按钮的时候,通过 $scope.$emit('to-parent', $scope.headerTitle); 将要传递的数据传递到父级控制器,然后在父级控制器中,通过 $scope.$on('to-parent', function (d, data)来获取数据,然后在函数中调用 $scope.$broadcast('to-child', data);
将数据传递到子控制器,最后在子控制器中用$scope.$on('to-child', function (d, data) 来接收传来的数据。
如果想把js脚本单独放到js文件中,可以这样:但要注意的是,所有的js文件的引用都是在父级页面
AngularJs 的ng-include指令的使用的更多相关文章
- 浅谈JSP中include指令与include动作标识的区别
JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...
- AngularJS常用插件与指令收集
angularjs 组件列表 bindonce UI-Router Angular Tree angular-ngSanitize模块-$sanitize服务详解 使用 AngularJS 开发一个大 ...
- JSP(include指令与<jsp:include>动作的区别)
<%@ page language= "java" contentType="text/html;charset=UTF-8" %><html ...
- jsp include指令
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- include指令和include标签的区别
区别 类别 语法 发生作用时间 包含的内容 转化成Servlet 编译时间 运行时间 include指令 <%@ include file="" %> 页面交换 实际内 ...
- 使用-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 d ...
- include动作标记和include指令标记学习笔记
我的jsp学习参考书是耿祥义,张跃平编著的jsp大学使用教程这本书,我也向大家推荐这本书,我觉得这本书适合我的学习方式,知识的讲解透彻易懂. include指令标记 ...
- Java学习-035-JavaWeb_004 -- JSP include 指令
inclue 指令是将不同的文件插入到 JSP 网页中,这些文件可以是文本文件.HTML文件.JSP 文件,指令语法如下: <%@include file="相对路径"%&g ...
- 转: JSP中include指令和include动作的区别
include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改,否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如果 ...
- include指令和<jsp:include>标准动作
利用JSP的包含机制,可以有效的避免重复,把可重用的部分独立出去,使用include把它们包含到当前文件.JSP有两种包含机制:include指令和<jsp:include>标准动作. 1 ...
随机推荐
- Exchanger使用
Exchanger使用
- ubuntu hadoop伪分布式部署
环境 ubuntu hadoop2.8.1 java1.8 1.配置java1.8 2.配置ssh免密登录 3.hadoop配置 环境变量 配置hadoop环境文件hadoop-env.sh core ...
- hdu 4925 Apple Tree--2014 Multi-University Training Contest 6
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 Apple Tree Time Limit: 2000/1000 MS (Java/Others ...
- Struts 1 Struts 2
Key Technologies Primer https://struts.apache.org/primer.html Threads With Struts 1 you were require ...
- JDK各版本内容和新特性
JDK各版本内容和新特性 - yanlzhl - 博客园 https://www.cnblogs.com/yanlzhl/articles/5694470.html 版本JDK1.0:1995年 ...
- SoapUI中读取Office365邮件
常见邮件服务一般使用IMAP邮件访问协议,如果你所在公司更换到Office 365则需要另一个组件. Office 365使用的是Exchange Server电子邮件服务组件,需要微软的Jar包来支 ...
- ignore users and roles by filter in sql source control
https://www.red-gate.com/hub/product-learning/sql-source-control/source-controlling-database-permiss ...
- mysql数据库ip与字符串
Mysql自带的IP转换语句 inet_aton:将ip地址转换成数字型 inet_ntoa:将数字型转换成ip地址 //使用inet_aton函数,将字符串IP转换为整型: mysql> se ...
- BZOJ4259:残缺的字符串(FFT与字符串匹配)
很久很久以前,在你刚刚学习字符串匹配的时候,有两个仅包含小写字母的字符串A和B,其中A串长度为m,B串长度为n.可当你现在再次碰到这两个串时,这两个串已经老化了,每个串都有不同程度的残缺. 你想对这两 ...
- [USACO 2004DEC] Navigation Nightmare
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3362 [算法] 带权并查集 时间复杂度 : O(NlogN) [代码] #inclu ...