本文介绍了如何手动重新运行格式化链与ngModel angularjs指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Angular.js ngModel有权宣布和的。可以在伟大的回答如何做双向滤波angular.js?

Angular.js ngModel has the ability to declare a chain of parsers and formatters. Some more details can be found at the great answer to 'How to do two-way filtering in angular.js?'

现在格式化链将只运行,如果ngModel将更新。
所以如果你有第二个输入参数,影响viewValue这(在格式化的一个被使用)不会触发视图的更新。
至于我发现ngModel只使用一个简单的$手表类似 - 所以,如果你的模型是一个集合/对象,如果子元素改变不会触发

now the formatter chain only will be run if the ngModel will update.so if you have a second input-parameter that affects the viewValue (is used in one of the formatters) this will not trigger an update of the View.similar as far as i found ngModel only uses a simple $watch - so if your model is a collection/object it will not trigger if sub-elements are changed.

什么是实施ngModel了深刻的手表最好的方式 - 结果
或为附加参数的手表应该重新运行格式化链?

还有其他类似的问题:结果
Angularjs:如何重新运行$格式化时一些设置被更改?

there are other similar questions:
Angularjs: how to "rerun" $formatters when some setting is changed?

推荐答案

目前还没有直接的API调用内部格式化链。
有此一。作为变通,你只可以复制内部code:

currently there is no direct api to call the internal formatter chain.there is a github feature request for this. as work-around you just can copy the internal code:

function runFormatters(ctrl){
    // this function is a copy of the internal formatter running code.
    // https://github.com/angular/angular.js/issues/3407#issue-17469647

    var modelValue = ctrl.$modelValue;

    var formatters = ctrl.$formatters;
    var idx = formatters.length;

    var viewValue = modelValue;

    while (idx--) {
        viewValue = formatters[idx](viewValue);
    }

    if (ctrl.$viewValue !== viewValue) {
        ctrl.$viewValue = ctrl.$$lastCommittedViewValue = viewValue;
        ctrl.$render();

        ctrl.$$runValidators(modelValue, viewValue, angular.noop);
    }

}

与其他参数的手表演示了使用组合

this Plunker demonstrates the usage in combination with a watch for additional parameters:

// deepwatch all listed attributes
scope.$watch(
    function(){
        return [scope.extraThingToWatchFor, scope.someOther];
    },
    function() {
        console.log("\t runformatters()");
        runFormatters();
    },
    true
);

证明上ngModel的deepwatch

this is a second Plunker to demonstrate the deepwatch on ngModel

// deepwatch ngModel
scope.$watch(
    function(){
        return ngModelCtrl.$modelValue;
    },
    function(newData) {
        runFormatters(ngModelCtrl);
    },
    true
);

这篇关于如何手动重新运行格式化链与ngModel angularjs指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 04:20