首页 > 其他分享 >angular的DEMO(用来练习和顺便看看)

angular的DEMO(用来练习和顺便看看)

时间:2023-01-13 15:34:22浏览次数:39  
标签:function return DEMO app 练习 value scope angular gi

  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入门很简单的,但是提升比较难吧

天道酬勤



标签:function,return,DEMO,app,练习,value,scope,angular,gi
From: https://blog.51cto.com/u_12304260/6006075

相关文章

  • angular的编辑器tinymce
    angular的插件的确挺少的,编辑器更是少,ui-tinymce是angular-ui推荐的一款编辑器​​(GIT:https://github.com/angular-ui/ui-tinymce)​​;效果图通过n......
  • angular实例教程(用来熟悉指令和过滤器的编写)
    angular的插件太少了, 所以很多指令和过滤器都要自己写, 所以对指令传进来的参数,以及angular编译的流程更加熟悉才行写出好的插件,有些简单的指令是参考​​angu......
  • angular的工具方法笔记(equals, HashKey)
    分别是angular脏值检测的工具方法equals和类HashKey的使用方法<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml......
  • angular的canvas画图例子
    angular的例子:<!DOCTYPEhtml><htmlng-app="APP"><head><metacharset="UTF-8"><scriptsrc="http://cdn.bootcss.com/angular.js/1.3.0-beta.12/angular.min.js"></......
  • angular例子笔记
    学习angular的插件写法和制作; <!DOCTYPEhtml><htmlng-app="APP"><head><metacharset="UTF-8"><title>angular-refreshexample</title><scriptsrc="h......
  • angularJS中-$route路由-$http(ajax)的使用
    后台请求使用的是nodeJS驱动(后面帖代码),很简单的RESTFUL,页面使用的是bottstarp3.0(懒人神器); 第一个例子:在本地架设NODEJS,angular的所有请求都是请求本地的300......
  • angular_$inject
    <!DOCTYPEHTML><htmllang="zh-cn"ng-app="MainApp"><head><metacharset="UTF-8"><title>explicit-inject-service</title></head><body><divng-controller="My......
  • Angularjs——初识AngularJS
    AngularJS——初识AngularJSAngularJS是什么AngularJS是Google开源的一款前端JS结构化框架,它通过对前端开发进行分层,极好地规范了前端开发的风格——它将前端开发分为Con......
  • Angular集成bpmn.js的基础实现及扩展(一)
    一、bpmn的基本认识bpmn.js是一个BPMN2.0渲染工具包和web建模器,使得画流程图的功能在前端来完成。bpmn画图具有哪些内容?二、使用npm安装bpmn.jsnpminstall--sav......
  • RHEL9练习部署pig4cloud
    手动安装便于学习理解本文记录安装过程和遇到问题,仅供复习和参考,有问题欢迎指正一、环境说明pig4cloud3.6的环境要求工具版本下载备注pig4cloud3.6https......