gpt4 book ai didi

angularjs - 如何在 AngularJS 中以不同方式显示模型和 View

转载 作者:行者123 更新时间:2023-12-04 21:08:02 25 4
gpt4 key购买 nike

我正在 AngularJS 中实现一个功能。
当用户输入 1.5 时,在 View 中,它应该显示为 01:30,但是当我在 Controller 中获取这个范围值时,它应该返回为 1.5。
我在plunker中添加了代码。请在这里找到。

索引.html:

<!DOCTYPE html>
<html ng-app="wbTimeConverter">

<head>
<link rel="stylesheet" href="style.css">
<script src="https://code.angularjs.org/1.5.8/angular.js"></script>
<script src="script.js"></script>
<script src="wbNumberToTime.js"></script>
</head>

<body ng-controller="AppController">
<h1>Hello Plunker!</h1>
<input type="text" md-maxlength="5" wb-number-to-time-convert ng-model="task" placeholder="task" ng-blur="onDataChange();" />

<input type="text" md-maxlength="5" wb-number-to-time-convert ng-model="project" placeholder="project" ng-blur="onDataChange();" />

<br>
<label>Task : {{task}}</label><br>
<label>Project : {{project}}</label><br>
<label>TotalResult : {{totalHours}}</label>
</body>

</html>

Controller - Script.js
var app = angular.module('wbTimeConverter', []);

app.controller('AppController', function($scope) {
$scope.onDataChange = onDataChange;

function onDataChange(){
console.log("res");
$scope.totalHours= parseFloat($scope.task) + parseFloat($scope.project, 10);
}


});

指令:
// 'use strict';
// /**
// * This directive is convert number into hours and minutes format-HH:MM
// * This will trigger when we change value in input element and gives respective value in time format
// */

app.directive('wbNumberToTimeConvert', function ($filter, $browser) {
return {
require: 'ngModel',
link: function ($scope, $element, $attrs, ngModelCtrl) {
var listener = function () {
var value = $element.val();
var result = convertToTime(value);
$element.val(result.timeFormat);
$element.attr('attr-hrs', result.decimalFormat);
};

// This runs when we update the text field
ngModelCtrl.$parsers.push(function (viewValue) {
return viewValue;
});

$element.bind('change', listener);
$element.bind('keydown', function (event) {
var key = event.keyCode;
// FIXME to handle validations
});

$element.bind('paste cut', function () {
$browser.defer(listener);
});

function convertToTime(value) {
var res = { 'timeFormat': '', 'decimalFormat': '' };
var inputValue = value;
if (inputValue.indexOf(':') > -1) {
inputValue = convertToNumberFormat(inputValue);
res.decimalFormat = inputValue;
} else {
res.decimalFormat = value;
}

inputValue = inputValue.split('.');
var hoursValue = inputValue[0];
if (inputValue.length > 1) {

var hrs = parseInt(hoursValue, 10);
hrs = isNaN(hoursValue) ? 0 : hrs;
hrs = (hrs < 10) ? '0' + hrs : hrs;

var minutesValue = inputValue[1];
var mins = parseInt(minutesValue, 10);
mins = (minutesValue.length < 2 && (mins < 10)) ? Math.round(mins * 6) : Math.round(mins * 0.6);
mins = (mins < 10) ? ('0' + mins) : mins;
inputValue = hrs + ':' + mins;
res.timeFormat = inputValue;
} else {

inputValue = (parseInt(inputValue, 10) < 10) ? '0' + parseInt(inputValue, 10) : parseInt(inputValue, 10);
inputValue = inputValue + ':' + '00';
res.timeFormat = inputValue;
}
return res;
}

function convertToNumberFormat(inputValue) {
var timeValue = inputValue.split(':');
var hours = parseInt(timeValue[0], 10);
var mins = parseInt(timeValue[1], 10);
if (isNaN(hours)){
hours = '00';
}
if (isNaN(mins)) {
mins = '00';
}
mins = Math.round(mins / 0.6);
if (mins < 10) {
mins = '0' + mins;
}
var number = hours + '.' + mins;
return number;
}

}

};
});

这是plunker链接:
https://plnkr.co/edit/76lwlnQlGC0wfjixicCK?p=preview

在文本框模糊上,第一次在 View 和 Controller 中的值不同,第二次在文本框模糊时,它在 View 和 Controller 中显示相同的值 01:30。我该如何解决?

最佳答案

您可以将输入保留在 ng-model myValue 中并调用函数 format(value)显示你需要的东西

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.myValue = "1.5";
$scope.format = function(value) {
var hrs = parseInt(Number(value));
var min = Math.round((Number(value) - hrs) * 60);
return hrs + ':' + min;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">

<input type="text" ng-model="myValue">
<br>Formatted Value : {{format(myValue)}}
<br>Base value : {{myValue}}
</div>

关于angularjs - 如何在 AngularJS 中以不同方式显示模型和 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41143856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com