加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在AngularJS中以不同方式显示模型和视图

发布时间:2020-12-17 07:15:23 所属栏目:安全 来源:网络整理
导读:我正在AngularJS中实现一个功能. 当用户输入1.5时,在视图中,它应显示为01:30,但是当我在控制器中获取此范围值时,它应返回1.5. 我在plunker中添加了代码.请在这里找到. index.html的: !DOCTYPE htmlhtml ng-app="wbTimeConverter" head link rel="stylesheet
我正在AngularJS中实现一个功能.
当用户输入1.5时,在视图中,它应显示为01:30,但是当我在控制器中获取此范围值时,它应返回1.5.
我在plunker中添加了代码.请在这里找到.

index.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>

控制器 – 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

在文本框模糊时,它在第一次和第二次在文本框中模糊时在视图和控制器中工作正常,它在视图和控制器中显示相同的值01:30.我该如何解决?

解决方法

您可以将输入保留在ng-model myValue中,并调用函数格式(值)来显示所需内容

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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读