inflector(辅助) 将用户输入的字符串转化成驼峰或者空格或者底线的小插件;

  这个是一个小的过滤器, 平常也是用不到的, 合格是过滤器的代码:

        app.filter("inflector", function() {
var reg = new RegExp("","gi");
return function(value ,type) {
switch( type ) {
case "underscore" :
value = value.replace(/[\s-_]/gi,"_");
break;
case "variable" :
value = value.replace(/[\s-_](\w)/gi,function($0,$1){
return $1.toUpperCase();
});
break;
default :
value = value.replace(/[\s-_]/gi," ");
break;
};
return value;
};
});

  下面的全部的代码,点击按钮即可在线运行:

<html ng-app="app">
<head>
<meta charset="utf-8" />
<script src="http://cdn.bootcss.com/jquery/2.1.1-rc2/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/angular.js/1.3.0-beta.13/angular.min.js"></script>
</head>
<body ng-controller="test0Controller">
<label>
<input type="radio" value="humanize" ng-model="inflectorType"> Humanize (Default)</label>
<label>
<input type="radio" value="underscore" ng-model="inflectorType"> Underscore</label>
<label>
<input type="radio" value="variable" ng-model="inflectorType"> Variable</label> <input placeholder="Enter some text to inflect" ng-model="inflectorText">
<p>{{inflectorText|inflector:inflectorType}}</p> <script>
//初始化用户模块;
var app = angular.module('app', []);
app.controller("test0Controller",function($scope){
$scope.inflectorText = "normal test_hehe-nice";
$scope.inflectorType = "humanize";
});
app.filter("inflector", function() {
var reg = new RegExp("","gi");
return function(value ,type) {
switch( type ) {
case "underscore" :
value = value.replace(/[\s-_]/gi,"_");
break;
case "variable" :
value = value.replace(/[\s-_](\w)/gi,function($0,$1){
return $1.toUpperCase();
});
break;
default :
value = value.replace(/[\s-_]/gi," ");
break;
};
return value;
};
});
</script>
</body>
</html>

  这一个实例主要想表达的是通过自定义的指令绑定事件, angular官方提供的指令也是这样子的:

<html ng-app="app">
<head>
<meta charset="utf-8" />
<script src="http://cdn.bootcss.com/jquery/2.1.1-rc2/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/angular.js/1.3.0-beta.13/angular.min.js"></script>
</head>
<body>
<div ng-controller="kepressController">
<textarea ui-keypress="keypressCallback">
</textarea>
</div>
<script>
//初始化用户模块;
var app = angular.module('app', []);
app.controller("kepressController",function($scope){
$scope.keypressCallback = function(e) {
e.target.value += "enter";
console.log(e)
alert("输入enter");
e.preventDefault();
};
}); app.directive("uiKeypress",function($parse) {
return {
scope : {
keypress : "&uiKeypress"
},
compile : function(elem, attrs) { return function(scope, $elem , $attrs ) {
$($elem).bind("keypress", function(ev) {
if( +ev.charCode === 13 ) {
scope.keypress()(ev);
};
});
}
}
}
}); </script>
</body>
</html>

  

  这个又是一个过滤器的例子,直接通过一个闭包创建一个排序的函数,简单粗暴:

<html ng-app="app">
<head>
<meta charset="utf-8" />
<script src="http://cdn.bootcss.com/jquery/2.1.1-rc2/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/angular.js/1.3.0-beta.13/angular.min.js"></script>
</head>
<body>
<div ng-controller="test0Controller">
<select ng-model="sortType">
<option value="firstName">firstName</option>
<option value="id">id</option>
<option value="gender">gender</option>
</select>
<div>
{{items | sort : sortType | json}}
</div>
</div>
<script>
//初始化用户模块;
var app = angular.module('app', []);
app.controller("test0Controller",function($scope){
$scope.inflectorText = "normal test_hehe-nice";
$scope.inflectorType = "humanize";
$scope.items = [
{ firstName: 'Dean', lastName: 'Sofer',
id: 1, gender: 'Male' },
{ firstName: 'Dean', lastName: 'Kuntz',
id: 2, gender: 'Male' },
{ firstName: 'Peter', lastName: 'Piper',
id: 3, gender: 'Female' },
{ firstName: 'Peter', lastName: 'Darwin',
id: 4, gender: 'Male' },
{ firstName: 'Janet', lastName: 'Piper',
id: 5, gender: 'Female' },
{ firstName: 'Dan', lastName: 'Doyon',
id: 6, gender: 'Male' },
{ firstName: 'Andy', lastName: 'Joslin',
id: 1, gender: 'Male' },
];
});
//排序的指令;
app.filter("sort",function() {
var sortClosure = function( name ){
return function(a,b) {
if( a[name] < b[name] ){
return -1;
}else{
return 1;
}
}
};
return function(value, type) {
var sortFn = sortClosure(type);
//return value.sort(sortFn);
return angular.copy(value).sort(sortFn);
};
}); </script>
</body>
</html>

   总结:angular入门很简单的,但是提升比较难吧

angular的DEMO(用来练习和顺便看看)的更多相关文章

  1. Angular - - 脏值检查及其相关

    今天突然就想写写$digest和$apply,这些都是脏值检查的主体内容. 先以普通js来做一个简单的监控例子吧: var div = ducoment.getElementById("my ...

  2. 初识Angular

    一.AngularJs简介 1.AngularJS使用了不同的方法,它尝试去补足HTML本身在构建应用方面的缺陷.AngularJS通过使用我们称为标识符(directives)的结构,让浏览器能够识 ...

  3. angular中的compile和link函数

    angular中的compile和link函数 前言 这篇文章,我们将通过一个实例来了解 Angular 的 directives (指令)是如何处理的.Angular 是如何在 HTML 中找到这些 ...

  4. anguar.js tutorial demo

    http://docs.angularjs.cn/tutorial angular 入门demo : PhoneCat Tutorial App 别人的DEMO(官方版):http://angular ...

  5. angular 滚动

    AngularJs $anchorScroll.$controller.$document $anchorScroll 根据HTML5的规则,当调用这个函数时,它检查当前的url的hash值并且滚动到 ...

  6. 说说Angular中的$timeOut定时器

     非常不幸的一点是,人们似乎常常将AngularJS中的$timeOut()函数看做是一个内置的.无须在意的函数.但是,如果你忘记了$timeOut()的回调函数将会造成非常不好的影响,你可能会因此遇 ...

  7. AngularJs angular.Module模块接口配置

    angular.Module Angular模块配置接口. 方法: provider(name,providerType); name:服务名称. providerType:创建一个服务的实例的构造函 ...

  8. AngularJs Angular数据类型判断

    angular.isArray 判断括号内的值是否为数组. 格式:angular.isArray(value); value: 被判断是否为数组的值. ------------------------ ...

  9. AngularJs angular.injector、angular.module

    angular.injector 创建一个injector对象, 调用injector对象的方法可用于获取服务以及依赖注入. 格式:angular.injector(modules); modules ...

随机推荐

  1. html3秒跳转

    <script>     setTimeout( 'window.location= "home.jsp " ',3000) ;//注意,此处“;”可加可不加</ ...

  2. SSIS OLEDB COMMAND RULES

    The oledb commad transformation prepare the wrong data type for the parameter. With my test, I have ...

  3. Web安全--XSS现代WAF规则探测及绕过技术

    XSS现代WAF规则探测及绕过技术初始测试 1.使用无害的payload,类似<b>,<i>,<u>观察响应,判断应用程序是否被HTML编码,是否标签被过滤,是否过 ...

  4. 频谱分析仪 RBW&VBW

    扫频式频谱分析仪的结构如下图 RBW(Resolution Bandwidth)的影响 The RBW dictates the resolution bandwidth, which is rela ...

  5. java操作excel文件

    采用POI操作excel API:http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFCell.html poi包:http ...

  6. 【问题&解决】解决 Android SDK下载和更新失败“Connection to https://dl-ssl.google.com refused”的问题

    缘由: 更新sdk,遇到了更新下载失败问题: Fetching https://dl-ssl.google.com/android/repository/addons_list-2.xmlFetche ...

  7. 边工作边刷题:70天一遍leetcode: day 78

    Graph Valid Tree 要点:本身题不难,关键是这题涉及几道关联题目,要清楚之间的差别和关联才能解类似题:isTree就比isCycle多了检查连通性,所以这一系列题从结构上分以下三部分 g ...

  8. <a href="javascript:void(0)" onclick="ff()" ></a> 用法解析

    javascript:void(0) 仅仅表示一个死链接 如果是个# javascript:void(#),就会出现跳到顶部的情况,搜集了一下解决方法 1:<a href="####& ...

  9. first root

    题目给出一棵二叉树的中序和后序排列.求出它的先序排列[提示]通过对比二叉树的中序和后序排列,我们可以找出根节点及左右子树.同样的,也可以通过对比左子树的中序和后序排列,找出左子树的根节点…….可见,该 ...

  10. Android service ( 二) 远程服务

    通常每个应用程序都在它自己的进程内运行,但有时需要在进程间传递对象,你可以通过应用程序UI的方式写个运行在一个不同的进程中的service.在android平台中,一个进程通常不能访问其他进程中的内存 ...