博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[AngularJS] ngModelController render function
阅读量:6113 次
发布时间:2019-06-21

本文共 4056 字,大约阅读时间需要 13 分钟。

ModelValue and ViewValue:


 

$viewValue: Actual string value in the view.

$modelValue: The value in the model that the control is bound to.

In Anuglar, it watchs the $modelValue for you and update $viewValue.

As you need to tell Angular when you set $viewValue and apply render() function to update.

 

Before the $render() function is called, the value will be passed into the $formatters.

$formatters: Array of functions to execute, as a pipeline, whenever the model value changes. The functions are called in reverse array order, each passing the value through to the next.

function formatter(value) {  if (value) {    return value.toUpperCase();  }}ngModel.$formatters.push(formatter);

 

Example: 

/** * Created by Answer1215 on 12/18/2014. */angular.module('app', [])    .directive('bank', function($filter) {        return{            restrict: 'E',            template: '
Click me to add $10 into your account
', require: 'ngModel', //The ^ prefix means that this directive searches for the controller on its parents (without the ^ prefix, the directive would look for the controller on just its own element) link: function(scope, element, attrs, ngModelCtrl) { /* ngModelCtrl.$formatters.push(function(modelValue) { return "$" + modelValue; });*/ //formatter is called before the render ngModelCtrl.$formatters.push($filter('currency')); //$render function require user to implement it ngModelCtrl.$render = function() { element.text('Now you have: ' + ngModelCtrl.$viewValue); } } } })
    

 

$rollbackViewValue(): Cancel an update and reset the input element's value to prevent an update to the $modelValue, which may be caused by a pending debounced event or because the input is waiting for a some future event.

If you have an input that uses ng-model-options to set up debounced events or events such as blur you can have a situation where there is a period when the $viewValue is out of synch with the ngModel's $modelValue.

In this case, you can run into difficulties if you try to update the ngModel's $modelValue programmatically before these debounced/future events have resolved/occurred, because Angular's dirty checking mechanism is not able to tell whether the model has actually changed or not.

The $rollbackViewValue() method should be called before programmatically changing the model of an input which may have such events pending. This is important in order to make sure that the input field will be updated with the new model value and any pending operations are cancelled.

See: 

 

$sec service: We are using the  service here and include the  module to automatically remove "bad" content like inline event listener (e.g.<span onclick="...">). However, as we are using $sce the model can still decide to provide unsafe content if it marks that content using the $sce service.

angular.module('customControl', ['ngSanitize']).directive('contenteditable', ['$sce', function($sce) {  return {    restrict: 'A', // only activate on element attribute    require: '?ngModel', // get a hold of NgModelController    link: function(scope, element, attrs, ngModel) {      if (!ngModel) return; // do nothing if no ng-model      // Specify how UI should be updated      ngModel.$render = function() {        element.html($sce.getTrustedHtml(ngModel.$viewValue || ''));      };      // Listen for change events to enable binding      element.on('blur keyup change', function() {        scope.$evalAsync(read);      });      read(); // initialize      // Write data to the model      function read() {        var html = element.html();        // When we clear the content editable the browser leaves a 
behind // If strip-br attribute is provided then we strip this out if ( attrs.stripBr && html == '
' ) { html = ''; } ngModel.$setViewValue(html); } } };}]);

 

转载地址:http://izcka.baihongyu.com/

你可能感兴趣的文章
C 函数sscanf()的用法
查看>>
python模块之hashlib: md5和sha算法
查看>>
linux系统安装的引导镜像制作流程分享
查看>>
解决ros建***能登录不能访问内网远程桌面的问题
查看>>
pfsense锁住自己
查看>>
vsftpd 相关总结
查看>>
bash complete -C command
查看>>
解决zabbix 3.0中1151端口不能运行问题
查看>>
售前工程师的成长---一个老员工的经验之谈
查看>>
Get到的优秀博客网址
查看>>
dubbo
查看>>
【Git入门之四】操作项目
查看>>
老男孩教育每日一题-第107天-简述你对***的理解,常见的有哪几种?
查看>>
Python学习--time
查看>>
在OSCHINA上的第一篇博文,以后好好学习吧
查看>>
高利率时代的结局,任重道远,前途叵测
查看>>
Debian 6.05安装后乱码
查看>>
欢迎大家观看本人录制的51CTO精彩视频课程!
查看>>
IntelliJ IDEA中设置忽略@param注释中的参数与方法中的参数列表不一致的检查
查看>>
关于软件开发的一些感悟
查看>>