总结一下:
- 格式化程序更改模型值在视图中的显示方式。
- 解析器更改视图值在模型中的保存方式。
这是一个简单的示例,以NgModelController
api文档中的示例为基础:
//format text going to user (model to view) ngModel.$formatters.push(function(value) { return value.toUpperCase(); }); //format text from the user (view to model) ngModel.$parsers.push(function(value) { return value.toLowerCase(); });您可以看到它的运行情况:http :
//plnkr.co/UQ5q5FxyBzIeEjRYYVGX?plnkr=legacy
<input type="button" value="set to 'misko'" ng-click="data.name='misko'"/><input type="button" value="set to 'MISKO'" ng-click="data.name='MISKO'"/><input changecase ng-model="data.name" />
在(查看模型)中键入名称时,您将看到模型始终是小写字母。但是,当您单击按钮并以编程方式更改名称(要查看的模型)时,输入字段始终为大写。



