AngularJS API

7、其他一些常用指令,布尔类型的指令也可以用表达式

  (1)、数组索引$index

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myController">
<p ng-repeat="x in names">{{$index+1}}_{{x}}</p>
</div>
<script>
angular.module('myApp', []).controller('myController', function ($scope) {
$scope.names=['Tom','Lily','John'];
});
</script>
</body>
</html>

$index指令

  (2)、ng-disabled对应HTML元素disable属性

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="">
<!--ng-model绑定的是checkbox的value值
ng-disabled对应button的disabled属性
-->
<input type="checkbox" ng-model="mySwitch"/>
<button ng-disabled="mySwitch">按钮</button>
</div>
</body>
</html>

ng-disabled

  (3)、ng-show、ng-hide指令

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="">
<input type="checkbox" ng-model="mySwitch"/>
<button ng-show="mySwitch">按钮1</button>
<button ng-hide="mySwitch">按钮2</button>
<input type="text" ng-model="hour"/>
<button ng-hide="hour>10">按钮3</button>
</div>
</body>
</html>

ng-show、ng-hide

  (4)、ng-click

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myController">
<button ng-click="count=count+1">{{count}}</button>
<input type="button" ng-click="toggle()" value="toggle"/> <div ng-show="switch" style="height: 20px;width: 20px;background-color: beige;border: 1px solid gray"> </div>
</div>
<script type="text/javascript">
angular.module('myApp', []).controller('myController', function ($scope) {
$scope.count = 0;
$scope.switch = true;
$scope.toggle = function () {
$scope.switch = !$scope.switch;
};
});
</script>
</body>
</html>

ng-click

 (5)、ng-include

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp4" ng-controller="myController4">
<div ng-include="'/test/angularjsInclude.html'">
</div>
</div>
<script type="text/javascript">
angular.module('myApp4',[]).controller('myController4',function(){ });
</script>
</body>
</html>

ng-include

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
AngularJSInclude
</body>
</html>

/test/angularjsInclude.html页面

8、AngularJS依赖注入,value、factory、service、provider、constant五个核心组件作为依赖注入

  (1)、value

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myController">
<input type="text" ng-model="initVal"/>
</div>
<script type="text/javascript">
var myApp=angular.module('myApp',[]);
myApp.value('initVal',5);
myApp.controller('myController',function($scope,initVal){
$scope.initVal=initVal;
});
</script>
</body>
</html>

依赖注入value

  (2)、factory

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myController">
initVal1:<input type="text" ng-model="initVal1"/>
<br/>
initVal2:<input type="text" ng-model="initVal2"/>
</div>
<script type="text/javascript">
var myApp = angular.module('myApp', []);
myApp.value('initVal', 5);
myApp.factory('myFactory', function (initVal) {
var factory = {};
factory.multiply1 = function () {
return initVal * initVal;
};
factory.multiply2=function(a,b){
return a*b;
};
return factory;
});
myApp.controller('myController', function ($scope, myFactory) {
$scope.initVal1 = myFactory.multiply1();
$scope.initVal2=myFactory.multiply2(2,3);
});
</script>
</body>
</html>

依赖注入factory

  (3)、service

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myController">
initVal1:<input type="text" ng-model="initVal"/>
</div>
<script type="text/javascript">
var myApp = angular.module('myApp', []);
myApp.value('initVal', 5);
myApp.factory('myFactory', function (initVal) {
var factory = {};
factory.multiply1 = function () {
return initVal * initVal;
};
factory.multiply2 = function (a, b) {
return a * b;
};
return factory;
});
myApp.service('myService', function (initVal, myFactory) {
this.squre = function (a) {
return myFactory.multiply2(a, a);
};
});
myApp.controller('myController', function ($scope, myService) {
console.log(myService);
$scope.initVal = myService.squre(3);
});
</script>
</body>
</html>

依赖注入service

  (4)、provider

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myController">
initVal1:<input type="text" ng-model="initVal"/>
</div>
<script type="text/javascript">
var myApp = angular.module('myApp', []);
myApp.config(function ($provide) {
$provide.provider('MyProvideService', function () {
this.$get = function () {
var factory = {};
factory.multiply = function (a, b) {
return a * b;
};
return factory;
};
});
});
myApp.controller('myController', function ($scope, MyProvideService) {
console.log(MyProvideService);
$scope.initVal = MyProvideService.multiply(2,3);
});
</script>
</body>
</html>

依赖注入provide

  (5)、constant常量

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myController">
initVal1:<input type="text" ng-model="initVal"/>
</div>
<script type="text/javascript">
var myApp = angular.module('myApp', []);
myApp.constant('myConstant', 23);
myApp.controller('myController', function ($scope, myConstant) {
$scope.initVal = myConstant;
});
</script>
</body>
</html>

依赖注入constant

9、AngularJS 路由

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<script src="http://apps.bdimg.com/libs/angular-route/1.3.13/angular-route.js"></script>
</head>
<body>
<div ng-app="myApp">
<ul>
<li><a href="#/">首页</a></li>
<li><a href="#/computers">电脑</a></li>
<li><a href="#/printers">打印机</a></li>
<li><a href="#/others">其他</a></li>
</ul>
<div ng-view></div>
</div>
<script type="text/javascript">
var myApp=angular.module('myApp',['ngRoute']);
myApp.config(['$routeProvider',function($routeProvider){
$routeProvider.when('/',{template:'这是首页'})
.when('/computers',{template:'电脑页面'})
.when('/printers',{template:'打印机页面'})
.otherwise({redirectTo:'/'});
}]);
</script>
</body>
</html>

路由简单例子

依赖angular-route.js脚本

初始化应用程序时,要导入ngRoute模块

AngularJS路由URL是通过#+标记实现的,在向服务端发送请求地址时,#符号后面的内容会被浏览器忽略掉

在HTML中#符号是超链接的锚点,如<a href="#top">置顶</a>,表示跳转到当前页面id或name为top的元素位置处,如果在浏览器地址栏中添加跳转到另一页面的锚点,那么另一页面元素要用id来指定锚点名称,如http://192.168.0.137:8080/anotherPage.html#bottom,会跳转到anotherPage.html页面id为bottom处。注意如果想传参数,要在#符号前传入

路由到的地址内容显示在ng-view指令标记的元素内

$routeProvider的两个函数(1)、when有两个参数,第一个是URL正则规则,第二个是路由配置对象(2)、otherwise有一个参数,是路由配置对象

路由设置对象:

$routeProvider.when(url, {
template: string,
templateUrl: string,
controller: string, function 或 array,
controllerAs: string,
redirectTo: string, function,
resolve: object<key, function>
});

template:ng-view内插入简单HTML内容

templateUrl:ng-view内插入HTML模板,如'/test/content.html',会从服务端获取到文件内容插入到ng-view内

controller:在当前模板上执行controller函数,生成新的$scope

controllerAs:为controller指定别名

redirectTo:重新定向到指定地址

resolve:指定当前controller所依赖的其他模块

angularjs笔记(三)的更多相关文章

  1. Mastering Web Application Development with AngularJS 读书笔记(三)

    第一章笔记 (三) 一.Factories factory 方法是创建对象的另一种方式,与service相比更灵活,因为可以注册可任何任意对象创造功能.例如: myMod.factory('notif ...

  2. angular学习笔记(三十一)-$location(2)

    之前已经介绍了$location服务的基本用法:angular学习笔记(三十一)-$location(1). 这篇是上一篇的进阶,介绍$location的配置,兼容各版本浏览器,等. *注意,这里介绍 ...

  3. Oracle学习笔记三 SQL命令

    SQL简介 SQL 支持下列类别的命令: 1.数据定义语言(DDL) 2.数据操纵语言(DML) 3.事务控制语言(TCL) 4.数据控制语言(DCL)  

  4. 《CMake实践》笔记三:构建静态库(.a) 与 动态库(.so) 及 如何使用外部共享库和头文件

    <CMake实践>笔记一:PROJECT/MESSAGE/ADD_EXECUTABLE <CMake实践>笔记二:INSTALL/CMAKE_INSTALL_PREFIX &l ...

  5. Python 学习笔记三

    笔记三:函数 笔记二已取消置顶链接地址:http://www.cnblogs.com/dzzy/p/5289186.html 函数的作用: 給代码段命名,就像变量給数字命名一样 可以接收参数,像arg ...

  6. 《MFC游戏开发》笔记三 游戏贴图与透明特效的实现

    本系列文章由七十一雾央编写,转载请注明出处. 313239 作者:七十一雾央 新浪微博:http://weibo.com/1689160943/profile?rightmod=1&wvr=5 ...

  7. [Firefly引擎][学习笔记三][已完结]所需模块封装

    原地址:http://www.9miao.com/question-15-54671.html 学习笔记一传送门学习笔记二传送门 学习笔记三导读:        笔记三主要就是各个模块的封装了,这里贴 ...

  8. JSP学习笔记(三):简单的Tomcat Web服务器

    注意:每次对Tomcat配置文件进行修改后,必须重启Tomcat 在E盘的DATA文件夹中创建TomcatDemo文件夹,并将Tomcat安装路径下的webapps/ROOT中的WEB-INF文件夹复 ...

  9. 构建高性能WEB站点笔记三

    构建高性能WEB站点笔记三 第10章 分布式缓存 10.1数据库的前端缓存区 文件系统内核缓冲区,位于物理内存的内核地址空间,除了使用O_DIRECT标记打开的文件以外,所有对磁盘文件的读写操作都要经 ...

随机推荐

  1. 模块加载(require)及定义(define)时的路径

    最近新公司在用requireJS进行JS的整合,刚开始接触有点蒙,于是深入了解了一下.requireJS主要是为了解决一下两个问题: (1)实现js文件的异步加载,避免网页失去响应: (2)管理模块之 ...

  2. MyBatis学习--延迟加载

    简介 在resultMap可以实现高级映射(使用association.collection实现一对一及一对多映射),association.collection具备延迟加载功能.例如:我们查询订单并 ...

  3. MySQL server PID file could not be found!

    重启mysql提示MySQL server PID file could not be found! Starting MySQL...The server quit without updating ...

  4. [转]理解JavaScript中的事件处理

    什么是事件? 事件(Event)是JavaScript应用跳动的心脏 ,也是把所有东西粘在一起的胶水.当我们与浏览器中 Web 页面进行某些类型的交互时,事件就发生了.事件可能是用户在某些内容上的点击 ...

  5. 网络流最小割 POJ 3469

    题意  2个CPU n个任务 给出在第一个 第二个运行时的花费 m  个  a  b 不在同一个CPU运行的额外花费 建图 源点 ->   n    -> 汇点 权          a1 ...

  6. 如何用css画出三角形

    看到有面试题里会有问到如何用css画出三角形 众所周知好多图形都可以拆分成三角形,所以说会了画三角形就可以画出很多有意思的形状 画出三角形的原理是调整border(边框)的四个方向的宽度,线条样式以及 ...

  7. Handler的postDelayed方法

    这是一种可以创建多线程消息的函数 使用方法:1,首先创建一个Handler对象 Handler handler=new Handler(); 2,然后创建一个Runnable对象 Runnable r ...

  8. [日常训练]常州集训day2

    T1 Description 给定$N$个点,问这$N$个点能构成的三角形个数. Input 第一行一个整数$N$,代表点数. 接下来$N$行,每行两个非负整数$X,Y$,表示一个点的坐标. Outp ...

  9. ——YC,你学到了吗?——学到了学到了

    又是周末了,想着开始我的每周一切(每周做一次从切图到静态网页布局的练习)任务吧,无意间看了看别人的页面,发现--我去,这个动画挺有意思的啊,怎么实现的?然后翻代码+搜索,啊,原来是插件啊~给我的也用上 ...

  10. 【BZOJ-3555】企鹅QQ 字符串Hash

    3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1545  Solved: 593[Submit][Statu ...