- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试实现 INR(印度卢比)到 USD(美元)的货币转换器。 View 应始终显示以 INR 为单位的值。但该模型应保存美元值(value)。
为此,我实现了一个用于输入的文本框。输入始终以 INR 为单位。
我正在使用 ngModel 的 $viewValue 和 $modelValue 属性来处理我的问题。
我遇到了一种情况,货币是在某些事件的后台计算的。例如。如果货币在模型中存储为 1 美元。在应用程序中的某些事件中,它会更改为 2 美元。在这种情况下,我的 View 显示以美元为单位的值(在本例中为 2 美元),并且仅当我关注文本框时,该值才会以 INR 为单位显示(如 126 INR)。
$viewValue 未在更改事件的文本框中显示。
请帮助我。
.directive('usdInrInput', function($filter, $timeout) {
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
function conversionFunction() {
modelCtrl.$viewValue = modelCtrl.$modelValue * 63;
modelCtrl.$render();
}
element.bind("focus", function(e) {
$timeout(function() {
conversionFunction();
}, 0);
});
element.bind("change", function(e) {
$timeout(function() {
conversionFunction();
}, 0);
});
modelCtrl.$parsers.push(function(inputValue) {
var changedOutput = parseInt(inputValue) / 63;
modelCtrl.$setViewValue(parseInt(inputValue));
modelCtrl.$render();
return parseInt(changedOutput);
});
}
};
})
最佳答案
您应该使用 scope.$watch
监视模型值的变化,如下所示:
scope.$watch(function() {
return modelCtrl.$modelValue;
}, function(val) {
conversionFunction();
});
使用美元汇率常量,以便在发生变化时可以在一处进行修改。
使用 $evalAsync
而不是 $timeout(function(){},0)
。
出于演示目的,我故意使用 $timeout
在 2 秒后更改了模型值。
angular
.module('myApp', []);
angular
.module('myApp')
.controller('MyController', MyController)
.directive('usdInrInput', usdInrInput);
MyController.$inject = ['$scope', '$timeout'];
function MyController($scope, $timeout) {
$scope.inr = 630;
$timeout(function() {
$scope.inr = 10;
}, 2000);
}
usdInrInput.$inject = ['$filter', '$timeout'];
function usdInrInput($filter, $timeout) {
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
var cRate = 63;
scope.$watch(function() {
return modelCtrl.$modelValue;
}, function(val) {
conversionFunction();
});
function conversionFunction() {
modelCtrl.$viewValue = modelCtrl.$modelValue * cRate;
modelCtrl.$render();
}
element.bind("focus", function(e) {
scope.$evalAsync(function() {
conversionFunction();
});
});
element.bind("change", function(e) {
scope.$evalAsync(function() {
conversionFunction();
});
});
modelCtrl.$parsers.push(function(inputValue) {
var changedOutput = parseInt(inputValue) / cRate;
modelCtrl.$setViewValue(changedOutput);
modelCtrl.$render();
return changedOutput;
});
}
};
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyController as MC">
<input type="text" ng-model="inr" usd-inr-input> {{inr}}
</div>
关于javascript - Angular $viewValue 未反射(reflect)在更改事件的文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42825677/
尝试创建 mask 控制台打印 $viewValue 不同但不适用于 View 。 NgModelController {$viewValue: "656-56", $modelValue: "656
我是 Angular 新手,正在尝试创建一个预输入输入,该输入仅显示以用户键入的内容开头的结果。例如,在我的 plunker 中,当用户输入“n”时,所有包含“n”的结果都会显示,而不仅仅是以字母开头
在 Angular 1.2 中,我需要能够更新 $modelValue 而不影响 $viewValue。我希望内部值是一个值,而用户看到的值是另一个值。我遇到的问题是设置 $modelValue 最终
使用自定义指令,添加验证函数以验证整数输入 var INTEGER_REGEXP = /^\-?\d+$/; app.directive('integer', function() { retur
我想从不同的指令读取初始值 ngModel.$viewValue。 # coffee script app.directive 'directive', -> return { req
如何在 Controller 中访问 $ngmodel.$viewValue?我有一个 ui-mask,但它没有为 ng-model 生成正确的值,因此我想提取从输入字段中看到的值。 最佳答案 我相信
我的复选框的默认值为“true”,我在其他地方以相同的形式使用复选框的 $viewValue。在应用程序开始时,复选框的 $viewValue 显示“false”,即使复选框被选中,但只有在用户关闭/
我是Angularjs的初学者,如果理解有误,请指正。 首先,下面是我的代码。我正在尝试创建一个用于输入验证的自定义指令。 For example: //if the length of the i
我正在使用 Angular JS 1.5.6,我想以编程方式取消同步 $viewValue 和 $modelValue,我希望 $viewValue 保持其当前值,但将 $modelValue 设置为
我正在尝试实现 INR(印度卢比)到 USD(美元)的货币转换器。 View 应始终显示以 INR 为单位的值。但该模型应保存美元值(value)。 为此,我实现了一个用于输入的文本框。输入始终以 I
我有一个 Angular 应用程序设置如下。 NG-repeat 包含 li 元素,每个元素有 2 个输入(远远超过 2 个,但对于这个演示,我有 2 个)。 该指令设置 Controller 。 H
我在输入上使用去抖: 我有一个指令来处理禁用此输入的递增按钮和递减按钮: app.directive('qtyInput', function() { return { re
我没有设法让 filter:$viewValue 在输入中使用 typeahead 工作。 我显示了整个列表,其中突出显示了输入的字符。 我希望根据输入的字符过滤列表。 这是一个plunker:htt
假设我有以下指令: myApp.directive('myDirective', function() { return { restrict: 'A', re
我正在尝试在我的 ngViews 中设置第二个 typeahead,尽管我的代码几乎与第一个相同,但第一个工作完美,而第二个将“未定义”传递给回调。 这是非功能性预输入的标记: Pick a
我有以下 ckEditor 指令。底部是我从有关如何在编辑器中设置数据的示例中看到的两种变体: app.directive('ckEditor', [function () { return
我正在尝试使用 http://angular-ui.github.io/bootstrap/ 在 Angular 中实现预输入 看起来应该很容易,但我收到以下错误: Global symbol $vi
我有一个动态表单,利用 ng-min/ng-max 进行验证。我的 ng-max 和 ng-min 分别绑定(bind)到对象参数 modelParam.maxvalue 和 modelParam.m
我有一个表单,其中的字段相互依赖,使用 UI Bootstrap 显示.从 bootstrap typeahead 中选择一个团队然后填充玩家列表,然后可以在第二个 typeahead 中选择。当用户
我需要能够暂时保留尚未完全验证的数据,然后在我准备好使其永久化时强制执行验证。但是 Angular 正在阻止这种情况。 我有一个表格。用户可以在表单的早期版本上saveDraft(),这些表单会持久保
我是一名优秀的程序员,十分优秀!