angularjs可交互的directive
angularjs可交互的directive
http://jsfiddle.net/revolunet/s4gm6/
directive开发上手练手,以注释的方式说明
html
<body ng-app="demo" ng-controller="demoController">
<h3>rn-stepper demo (1/5){{rating}}</h3>
Model value : {{ rating }}<br>
Form Has been modified : {{ form.$dirty }}
<hr>
<div ng-model="rating" rn-stepper></div>
</body>
css
body {
font-family: 'Roboto', sans-serif;
}
$stepper-height: 40px;
$stepper-value-width: 40px;
$stepper-button-width: 40px;
$stepper-border-width: 1px;
$stepper-button-bg: #4D4DFF;
$stepper-value-bg: #eee;
div[rn-stepper] {
border:1px solid #bbb;
display:inline-block;
height:$stepper-height + ($stepper-border-width * 2);
box-sizing:border-box;
button {
appearance:none;
-webkit-appearance:none;
margin:0;
border:0;
width: $stepper-button-width;
height:$stepper-height;
box-sizing:border-box;
background: $stepper-button-bg;
color: white;
font-weight:bold;
font-size:20px;
outline: none;
&:active {
box-shadow: inset 0 2px 2px rgba(0, 0, 0, 0.25);
background: darken($stepper-button-bg, 5%);
}
}
div {
vertical-align:top;
width:$stepper-value-width;
background:$stepper-value-bg;
text-align:center;
display:inline-block;
height:$stepper-height;
line-height:$stepper-height;
box-sizing:border-box;
}
}
javascript
angular.module('demo', [])
.controller('demoController',['$scope',function($scpoe){
$scpoe.rating=52;
}])
.directive('rnStepper', function() {
return {
restrict: 'AE',
require:'ngModel',/*使用属性模式调用,依赖了ngModel指令*/
scope:{},
template: '<button ng-click="decrement()">-</button>' +
'<div></div>' +
'<button ng-click="increment()">+</button>',
//link函数可以接受require指令的controller,ngModelController
link:function(scope,element,attrs,ngModelController){
//利用ngModel指令的controller我们可以利用他的方法很多事情
ngModelController.$render=function(){
element.find('div').text(ngModelController.$viewValue);
};
function updateModel(offset){
ngModelController.$setViewValue(ngModelController.$viewValue+offset);
ngModelController.$render();
};
scope.decrement=function(){
updateModel(-1);
};
scope.increment=function(){
updateModel(1);
};
}
};
});

学习的最好办法就是,多练习,照抄都行 -- 莎撤丹斯基
angularjs可交互的directive的更多相关文章
- AngularJS系统学习之Directive(指令)
本文转自https://www.w3ctech.com/topic/1612 原文作者: Nicolas Bevacqua 原文:AngularJS’ Internals In Depth, Part ...
- AngularJS入门心得1——directive和controller如何通信
粗略地翻了一遍<JavaScript DOM编程艺术>,就以为可以接过AngularJS的一招半式,一个星期过去了,我发现自己还是Too Young,Too Simple!(刚打照面的时候 ...
- angularjs学习笔记三——directive
AngularJS 通过被称为 指令 的新属性来扩展 HTML. 正如你所看到的,AngularJS 指令是以 ng 作为前缀的 HTML 属性. HTML5 允许扩展的(自制的)属性,以 data- ...
- AngularJS学习笔记之directive—scope选项与绑定策略
From:http://www.linuxidc.com/Linux/2015-05/116924.htm scope:{}使指令与外界隔离开来,使其模板(template)处于non-inherit ...
- 利用angularJs自定义指令(directive)实现在页面某一部分内滑块随着滚动条上下滑动
最近老大让我一个效果实现在页面某一部分内滑块随着滚动条上下滑动,说明一下我们项目使用技术angularJs.大家都知道,使用jquery很好实现. 那么angular如何实现呢,我用的是自定义指令(d ...
- AngularJS学习笔记之directive——scope选项与绑定策略
开门见山地说,scope:{}使指令与外界隔离开来,使其模板(template)处于non-inheriting(无继承)的状态,当然除非你在其中使用了transclude嵌入,这点之后的笔记会再详细 ...
- angularjs springMVC 交互
AngularJS中的$resource使用与Restful资源交互 1.AngularJS中的 $resource 这个服务可以创建一个资源对象,我们可以用它非常方便地同支持RESTful的服务端数 ...
- AngularJs(Part 11)--自定义Directive
先对自定义Directive有一个大体的映像 myModule.directive('myDirective',function(injectables){ var directiveDefiniti ...
- angularJs自定义指令(directive)实现滑块滑动
最近老大让我一个效果实现在页面某一部分内滑块随着滚动条上下滑动,说明一下我们项目使用技术angularJs.大家都知道,使用jquery很好实现. 那么angular如何实现呢,我用的是自定义指令(d ...
随机推荐
- INotifyPropertyChanged接口的PropertyChanged 事件
INotifyPropertyChanged 接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知. 例如,考虑一个带有名为 FirstName 属性的 Person 对象. 若要提供 ...
- MATLAB常用操作
1.点乘,点除,点乘方 点乘(对应元素相乘),必须同维或者其中一个是标量,a.*b 点除,a.\b表示矩阵b的每个元素除以a中对应元素或者除以常数a,a./b表示常数a除以矩阵b中每个元素或者矩阵a除 ...
- 结对项目——高级四则运算检验器记录(168 & 187)
首先,上图(*+﹏+*)~@ 1.如何看待结对编程 结对编程优点: 1.两个人能够相互支持,相互监督,客服编程过程中可能出现的烦躁的情况0_0. 2.在开发功能的同时,伴随了UnitTest的进行,可 ...
- Enable MFA for a user
If you are root/admin account, in order to configure a virtual MFA device, you must have physical ac ...
- [Leetcode]String to Integer (atoi) 简易实现方法
刚看到题就想用数组做,发现大多数解也是用数组做的,突然看到一个清新脱俗的解法: int atoi(const char *str) { ; int n; string s(str); istrings ...
- linux sort,uniq,cut,wc命令详解
linux sort,uniq,cut,wc命令详解 sort sort 命令对 File 参数指定的文件中的行排序,并将结果写到标准输出.如果 File 参数指定多个文件,那么 sort 命令将这些 ...
- ADO.NET Entity Framework
ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案, 早期被称为 ObjectSpace,现已经包含在 V ...
- highcharts 不显示X轴 Y轴 刻度
xAxis: { tickWidth:0, //设置刻度标签宽度 lineColor:'#ffffff',//设置坐标颜色 lineWidth:0, //设置坐标宽度 la ...
- 读《我是IT小小鸟》后有感
我是一名大一新生,在下半学期开学时,我迎来新课程——<大学生职业生涯规划与就业指导 >.这是一门既新颖,又有许多就业知识和理论的学科.在课上,老师向我们推荐了一本书,名叫<我是IT小 ...
- 开始VS 2012 中LightSwitch系列的第2部分:感受关爱——定义数据关系
[原文发表地址] Beginning LightSwitch in VS 2012 Part 2: Feel the Love - Defining Data Relationships [原文发表 ...