<select ng-model="model.id" convert-to-number>
<option value="0">Zero</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>
{{ model }}
angular.module('nonStringSelect', [])
.run(function($rootScope) {
$rootScope.model = { id: 2 };
})
.directive('convertToNumber', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, ngModel) {
//format text from the user (view to model)
ngModel.$parsers.push(function(val) { return parseInt(val, 10); }); 
//format text going to user (model to view)
ngModel.$formatters.push(function(val) { return '' + val; }); } }; });

Formatters Definition

Array of functions to execute, as a pipeline, whenever the model value changes. Each function is called, in turn, passing the value through to the next. Used to format / convert values for display in the control and validation.

Parsers Definition

Array of functions to execute, as a pipeline, whenever the control reads value from the DOM. Each function is called, in turn, passing the value through to the next. Used to sanitize / convert the value as well as validation. For validation, the parsers should update the validity state using $setValidity(), and return undefined for invalid values.

To summarize:

  • Formatters change how model values will appear in the view.
  • Parsers change how view values will be saved in the model.

ng-model值字符串转数值型(convertToNumber directive)的更多相关文章

  1. heap表按字符串和数值型排序规则

    SQL> create user scan identified by scan default tablespace users; User created. SQL> grant db ...

  2. MYSQL中的数值型数据类型与字符串类型

    /* 数值型数据类型主要用来存储数字,包含的类型有: TINYINT.SMALLINT.MEDIUMINT. INT(INTEGER). BIGINT TINGINT占1个字节,SMALLINT占2个 ...

  3. MySQL中的数据类型 [数值型、字符串型、时间日期型]

    MySQL中的数据类型 [数值型.字符串型.时间日期型] MySQL中各数据类型 1. 数值类型(整型) 类型 数据大小 类型 (无符号:unsigned) 数据大小 存储空间 tinyint -12 ...

  4. 字符串类型ip与数值型ip地址相互转换

    /** * 返回Integer类型的ip地址 * @return */ private static Integer ipToInt(){ String ip="192.168.1.201& ...

  5. 怎样验证layer.prompt输入的值为数值型???

    JS中使用isNaN()判断layer.prompt输入的值为数值型,代码如下: layer.prompt({ title: '设置比值', }, function(value, index, ele ...

  6. Docs-.NET-C#-指南-语言参考-关键字-内置类型-值类型:整型数值类型

    ylbtech-Docs-.NET-C#-指南-语言参考-关键字-内置类型-值类型:整型数值类型 1.返回顶部 1. 整型数值类型(C# 参考) 2019/10/22 “整型数值类型”是“简单类型”的 ...

  7. iOS学习之Object-C语言字符串和数值

    一.使用苹果帮助文档      1.帮助文档的作用:帮助开发者快速了解系统类的功能.           1)苹果每次iOS版本的升级,都会添加或者更新大量的API,并提供相应的参考文档.       ...

  8. js中字符串转换为数值的两种方法的区别

    在js中字符串转换为数值的方法有三种:转换函数,强制类型转换,隐式转换 1.转换函数 parseInt()   //将字符串转换为整型 parseFloat()  //将字符串转换为浮点型 转换函数在 ...

  9. mysql概要(二)类型(数值型,字符型,时间类型

    1.mysql数值型范围 tinyint可选属性 tinyint(N) unsigned zerofill N:表示显示长度,与zerofill配合使用,即长度不够用0填充,并且自动变成无符号的数,N ...

随机推荐

  1. AngularJS 动画总结

    对读过的几篇文章的总结,尽量保证逻辑性,不断补充.精简.更正. 后面会列出参考文章地址,方便以后取用.感谢各位作者以及翻译者. AngularJS 动画思考 一.如何使用 1)我们需要构建什么 2)如 ...

  2. BotVS开发基础—2.11 API绘制图表

    代码 import time chart = { '__isStock': True, # 标记是否为一般图表,有兴趣的可以改成 false 运行看看. 'tooltip': {'xDateForma ...

  3. java 面试,java 后端面试,数据库方面对初级和高级程序员的要求

    本内容摘自 java web轻量级开发面试教程 对于合格的程序员,需要有基本的数据库操作技能,具体体现在以下三个方面. l  第一,针对一类数据库(比如MySQL.Oracle.SQL Server等 ...

  4. java 面试,如何提升自己的实力,摘自 java web轻量级开发面试教程

    本内容摘自 java web轻量级开发面试教程 其中有一段讲述到了实习经验对找工作的帮助 1.2.2大学阶段的实习经验能帮到你 一般公司在筛选简历时,一个非常重要考察的要点是相关经验的工作年限,说一个 ...

  5. [Caffe]史上最全的caffe安装过程

    Linux下的GPU版Caffe安装方法 系统环境:Ubuntu 14.04LTS + NV TitanX 1.1 (可选)显卡驱动的安装(有风险) 如果需要重装,需要先卸载已有版本 sudo apt ...

  6. 使用build_opener 自定义 opener

    使用build_opener 自定义 opener,这种方法的好处是可以方便的拓展功能. import urllib.request import http.cookiejar def makeMyO ...

  7. Ubuntu & GitLab CI & Docker & ASP.NET Core 2.0 自动化发布和部署(1)

    相关博文: Ubuntu 简单安装和配置 GitLab Ubuntu 简单安装 Docker Ubuntu Docker 简单安装 GitLab Ubuntu Docker 安装和配置 GitLab ...

  8. Spring整合Redis(spring-data-redis)

    历经几天看了大量的博客资料,差不多算是搞定了,目前只是针对单个数据源,集群暂时没研究 maven依赖 <properties> <!-- redis 版本 --> <re ...

  9. 初学者入门web前端:C#基础知识:函数

    入行前端对函数的掌握程度有可能直接影响以后工作的效率,使用函数可以高效的编写编码,节省时间,所以我整理了C#中最基础的函数知识点,虽然我在学习中 遇到很多问题,但是只要能够解决这些问题,都是好的. 一 ...

  10. 运行mvn install时跳过Test

    1.1 方法一 <project> [...] <build> <plugins> <plugin> <groupId>org.apache ...