Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 使用格式化值更新视图,使用数值更新模型_Angularjs - Fatal编程技术网

Angularjs 使用格式化值更新视图,使用数值更新模型

Angularjs 使用格式化值更新视图,使用数值更新模型,angularjs,Angularjs,我需要输入框来显示根据用户的语言环境格式化的值,但是模型必须将值存储在en-US语言环境中,所有这些都发生在blur上。当用户单击这些字段时,这些字段的格式可以正常工作,但我不知道如何设置模型值。在我的代码中,FormattedValue设置正确,用户可以查看,但如何将模型值更新为“valueToFormat”?我试过了 scope.$modelValue = valueToFormat; 当通过调试器查看它时,它会工作,但一旦呈现视图,该值就会恢复为$viewValue。我怎样才能做到这一点

我需要输入框来显示根据用户的语言环境格式化的值,但是模型必须将值存储在en-US语言环境中,所有这些都发生在blur上。当用户单击这些字段时,这些字段的格式可以正常工作,但我不知道如何设置模型值。在我的代码中,FormattedValue设置正确,用户可以查看,但如何将模型值更新为“valueToFormat”?我试过了

scope.$modelValue = valueToFormat;
当通过调试器查看它时,它会工作,但一旦呈现视图,该值就会恢复为$viewValue。我怎样才能做到这一点

element.bind('blur', function () {
                var val = ctrl.$modelValue;
                parse(val);
            })

ctrl.$formatters.push(function(value) {
                if(!value) {
                    return value;
                }

                var valueToFormat = getActualValue(value, decimalDelimiter, thousandsDelimiter, decimals, '%') || '0';
                return viewMask.apply(prepareNumberToFormatter(valueToFormat, decimals));
            });

function parse(value) {
                if(!value) {
                    return value;
                }

                var valueToFormat = getActualValue(value, decimalDelimiter, thousandsDelimiter, decimals) || '0';
                var formatedValue = viewMask.apply(prepareNumberToFormatter(valueToFormat, decimals));
                var actualNumber = parseFloat(modelMask.apply(valueToFormat));

                ctrl.$viewValue = formatedValue;
                ctrl.$render();

                return valueToFormat;
            }

您应该能够使用过滤器来解决此问题。例如,这是我们使用的日期过滤器。这是使用typescript的,所以对纯java脚本进行一些调整是必要的,但这一点很重要

app.filter('MyDateFilter', ['$filter', function ($filter: ng.IFilterService)
        {
            return function (value:any, format)
            {
                if (typeof value == "string")
                {
                    var stringValue = <string>value;
                    //If there is no / or - then assume its not a date
                    if (stringValue.indexOf('/') == -1 && stringValue.indexOf('-') == -1)
                        return value;
                }
                var parsedDate = Date.parse(value);
                if (isNaN(parsedDate))
                    return value;
                else
                    return $filter('date')(value, format);
            }
        }]);
这将格式化您想要的值。如果您只希望在模糊上发生这种情况,则可以添加

 ng-model-options="{ updateOn: 'blur' }"

<input type="text" my-date-display-input  ng-model-options="{ updateOn: 'blur' }" ng-model="unformattedDate"/>
ng model options=“{updateOn:'blur'}”

有些部分可能丢失了,但这应该能让大家明白这一点。

为什么不使用过滤器呢?但我认为您不能在输入框上使用过滤器。您将如何将其应用于输入,并且仅在用户离开字段时应用(模糊)?答案已更改。
 app.directive("myDateDisplayInput", ['$filter', function DatepickerDirective(filter: ng.IFilterService): ng.IDirective
        {
            return {
                restrict: 'A',
                require: 'ngModel',
                link: (scope: ng.IScope, element, attrs, ngModelCtrl: ng.INgModelController) =>
                {


ngModelCtrl.$render = () => 
            {
                var date = ngModelCtrl.$modelValue ?
                    filter('date')(ngModelCtrl.$modelValue, "MM/dd/yyyy") :
                    ngModelCtrl.$viewValue;

                jqueryElement.val(date);
            };

}
                };
            }
 ng-model-options="{ updateOn: 'blur' }"

<input type="text" my-date-display-input  ng-model-options="{ updateOn: 'blur' }" ng-model="unformattedDate"/>