Angular.js进阶
1.常用指令
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="css/foundation.min.css">
<style>
.tx{
width: 50px;
height: 50px;
margin-bottom: 10px;
margin-left: 80px;
}
</style>
</head>
<!--ng-bind,{{}},ng-model,ng-show/hide,ng-if,ng-checked,ng-src-->
<!--ng-href,ng-class,ng-selected,ng-submit,ng-disable,ng-change-->
<body style="padding:10px" ng-app="app">
<div ng-controller="UserCtrl">
<form name="f" ng-submit="register(user)">
<fieldset>
<legend>基本信息</legend>
<img ng-src="{{user.icon}}"
ng-class="{'tx':user.showicon}"/>
<!--ng-hide="user.isShowImg"-->
<!--ng-show="user.isShowImg"-->
<!--ng-if="user.isShowImg"-->
<div>
<input type="text" ng-model="user.uname" placeholder="用户名" required />
<input type="password" placeholder="密码"/>
职位:<select>
<option value="">--请选择--</option>
<option value="1" ng-selected="user.zw=='1'">Java工程师</option>
<option value="2" ng-selected="user.zw=='2'">web工程师</option>
</select>
性别:<input type="radio"
ng-checked="user.sex=='1'"
name="sex"> 男
<input type="radio"
ng-checked="user.sex=='0'"
name="sex"> 女
</div>
</fieldset>
<fieldset>
<legend>爱好</legend>
<div>
<input type="checkbox" ng-checked="isChecked(1)" name="爱好"> 篮球
<input type="checkbox" ng-checked="isChecked(2)" name="爱好"> 足球
<input type="checkbox" ng-checked="isChecked(3)" name="爱好"> 排球
</div>
</fieldset>
<button type="submit"
ng-disabled="f.$invalid"
class="button expand">提交</button>
</form>
</div>
</body>
<script src="js/angular.min.js"></script>
<script>
angular.module('app',[])
.controller('UserCtrl',function($scope){
$scope.user={
isShowImg:true,
showicon:true,
icon:"images/demo.jpg",
uname:"",
pwd:"",
zw:"2",
sex:"1",
aihao:[1,3]
};
$scope.isChecked=function(n){
var isok=false;
for(var i=0;i<$scope.user.aihao.length;i++){
if(n==$scope.user.aihao[i]){
isok=true;
break;
}
}
return isok;
};
$scope.register=function(u){
console.log(u);
}
}); </script>
</html>
2.angular与element(1)
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="http://cdn.static.runoob.com/libs/foundation/5.5.3/css/foundation.min.css">
</head>
<body>
<div ng-app="app">
<div enter leave>我在这</div>
</div>
</body>
<script src="js/angular.min.js"></script>
<script>
var app=angular.module('app',[]);
app.directive('enter',function(){
return function(scope,element,attrs){
element.bind('mouseenter',function(){
element.addClass('alert-box');
})
}
});
app.directive('leave',function(){
return function(scope,element,attrs){
element.bind('mouseleave',function(){
element.removeClass('alert-box');
})
}
});
</script>
</html>
angular与element(2)
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="css/foundation.min.css">
</head>
<body>
<div ng-app="app">
<hello></hello>
</div>
</body>
<script src="js/angular.min.js"></script>
<script>
var app=angular.module('app',[]);
app.directive('hello',function(){
return {
restrict:"E",
template:'<div><input ng-model="txt"></div><div>{{txt}}</div>',
link:function(scope,element){
scope.$watch('txt',function(newVal){//监听
if(newVal==='error'){
element.addClass('alert-box alert')
}
})
}
}
});
</script>
</html>
3.自定义HTML组件
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="css/bootstrap.min.css"/>
</head>
<body>
<p>p:restrict含义(A属性,E标签元素(结合template插入),C类名)与应用,replace使用,template使用</p>
<div ng-app="app">
<div hello class="leiming"></div>
</div>
</body>
<script src="js/angular.min.js"></script>
<script>
var app=angular.module('app',[]);
app.directive('hello',function(){
// return {
// restrict:'E',//自定义HTML标签,在dom中显示标签名为hello
// replace:true,//默认false,替换掉自定义的directive名称,即把hello替换为div
// template:'<div>hello Angularjs</div>'
// };
return {
restrict:'A',
link:function(){
alert("我是属性")
}
};
});
app.directive('leiming',function(){
return {
restrict:'C',
link:function(){
alert("我是类名")
}
};
});
</script>
</html>
4.directive与controller
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<!--<p>controller属性使用,link使用,directive中调用controller的方法</p>-->
<div ng-app="app">
<!--<div ng-controller="AppCtrl">-->
<!--<div enter="loadMoreData()">加载数据</div>-->
<!--</div>-->
<food apple orange banana>所有食物</food>
<food apple banana>所有食物</food>
</div>
</body>
<script src="js/angular.min.js"></script>
<script>
var app=angular.module('app',[]);
//app.controller('AppCtrl',function($scope){
// $scope.loadMoreData=function(){
// alert("正在加载数据...")
// };
// $scope.delData=function(){
// alert("正在删除数据...")
// }
//});
//app.directive('enter',function(){
// return function(scope,element,attrs){
// element.bind('mouseenter',function(){
// //scope.loadMoreData();
// //scope.$apply('loadMoreData()');
// scope.$apply(attrs.enter);
// })
// }
//});
app.directive('food',function(){
return {
restrict:'E',
scope:{},
controller:function($scope){
$scope.foods=[];
this.addApple=function(){
$scope.foods.push('Apple');
};
this.addOrange=function(){
$scope.foods.push('Orange');
};
this.addBanana=function(){
$scope.foods.push('Banana');
}
},
link:function(scope,element,attrs){
element.bind('mouseenter',function(){
console.log(scope.foods);
});
}
}
});
app.directive('apple',function(){
return {
require:'food',
link:function(scope,element,attrs,foodCtrl){
foodCtrl.addApple();
}
}
});
app.directive('orange',function(){
return {
require:'food',
link:function(scope,element,attrs,foodCtrl){
foodCtrl.addOrange();
}
}
});
app.directive('banana',function(){
return {
require:'food',
link:function(scope,element,attrs,foodCtrl){
foodCtrl.addBanana();
}
}
});
</script>
</html>
Angular.js进阶的更多相关文章
- AngularJS进阶(四)ANGULAR.JS实现下拉菜单单选
ANGULAR.JS: NG-SELECT AND NG-OPTIONS PS:其实看英文文档比看中文文档更容易理解,前提是你的英语基础还可以.英文文档对于知识点讲述简明扼要,通俗易懂,而有些中文文档 ...
- Atitit.angular.js 使用最佳实践 原理与常见问题解决与列表显示案例 attilax总结
Atitit.angular.js 使用最佳实践 原理与常见问题解决与列表显示案例 attilax总结 1. 本文范围 1 2. Angular的优点 1 2.1. 双向数据绑定 1 2.2. dsl ...
- MVC、MVP、MVVM、Angular.js、Knockout.js、Backbone.js、React.js、Ember.js、Avalon.js、Vue.js 概念摘录
注:文章内容都是摘录性文字,自己阅读的一些笔记,方便日后查看. MVC MVC(Model-View-Controller),M 是指业务模型,V 是指用户界面,C 则是控制器,使用 MVC 的目的是 ...
- 2. web前端开发分享-css,js进阶篇
一,css进阶篇: 等css哪些事儿看了两三遍之后,需要对看过的知识综合应用,这时候需要大量的实践经验, 简单的想法:把qq首页全屏另存为jpg然后通过ps工具切图结合css转换成html,有无从下手 ...
- angular.js:13920 Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- testServe
angular.js:13920 Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- testSer ...
- (翻译)Angular.js为什么如此火呢?
在本文中让我们来逐步发掘angular为什么如此火: Angular.js 是一个MV*(Model-View-Whatever,不管是MVC或者MVVM,统归MDV(model Drive View ...
- angular.js写法不规范导致错误
以下写法:没有明确指定module和controller,写法不规范. 更改angular.js版本会出bug. <html ng-app> <head> <title& ...
- Angular.js实现折叠按钮的经典指令.
var expanderModule=angular.module('expanderModule',[]) expanderModule.directive('expander',function( ...
- Angular.js通过bootstrap实现经典的表单提交
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel= ...
随机推荐
- C# ——窗体和控件随着分辨率的变化自适应大小
一.说明 我们自己编写程序的界面,会遇到各种屏幕分辨 率,只有自适应才能显的美观.实际上,做到这点也很简单,就是首先记录窗体和它上面控件的初始位置和大小,当窗体改变比例时,其控件的位置和大小也按此比 ...
- IE的兼容性设置 X-UA-Compatible
< meta http-equiv = "X-UA-Compatible" content = "IE=edge,chrome=1" /> 是IE8 ...
- mysql 修改数据库密码
MYSQL5.7以下版本的数据库密码使用的是 mysql这个数据库里的user表的password这个字段, 修改密码只需: 1.update MySQL.user set password=pass ...
- tcp三次握手和四次挥手(2)
背景描述 通过上一篇中网络模型中的IP层的介绍,我们知道网络层,可以实现两个主机之间的通信.但是这并不具体,因为,真正进行通信的实体是在主机中的进程,是一个主机中的一个进程与另外一个主机中的一个进 ...
- 微软发布SQL Server on Linux
本文参考并翻译自:微软云计算与企业执行副总裁Scott Guthrie的博客. 过去的一年,不管是对于微软的数据业务,还是整个行业,都是令人惊喜的一年.在周四刚于纽约举行的Data Driven活动中 ...
- Eclipse如何设置编译文件.class输出路径
1.首先我发现我的eclipse中-->project-->build automatically 是勾选上的.好吧,把把前面的勾去掉. 2去掉以后我先clean --> Clean ...
- c# 知识学习
1.C#基础知识梳理系列 2.详解C#委托,事件与回调函数 3.C#制作Windows service
- SAP Cloud for Customer Sales Order Requested Date的业务含义和实现
我们在创建Sales order销售订单时,需要指定一个RequestedDate: 这个字段绑定到了BO字段:CustomerQuote.RequestedFulfillmentPeriod.Tim ...
- 第二次作业——MathExamLv2
MathExamLv2--林志松 211406285 李明康 211606314 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际 ...
- c++11 多线程新特性学习 (1) 管理线程
1.基础介绍 c++11中,线程是通过std::thread对象来开始的,用法为 #include<thread> //必须包含的头文件 void do_work(){ std::cout ...