如何在yardjs中将码数转换为米值

时间:2017-06-15 11:35:54

标签: html angularjs valueconverter

这里是尝试将仪表值转换为码数。我有两个下拉菜单。第一次下拉有一些价值。第二个下拉列表有两个值"米" &安培; "码",现在问题是如果从第二个下拉列表中选择,第一个下拉列表中的值想要保持相同。但是,如果选择" Yard" ,则需要将第一个下拉列表中的所有值转换为Yard值。我找到了逻辑,但我不知道如何将它实现到角度控制器中。这里创建了一个plunkr https://plnkr.co/edit/Q4lxXf6YRrdAV2kUNAln?p=preview

HTML

<!DOCTYPE html>
<html>

  <head>
    <script data-require="angular.js@1.6.2" data-semver="1.6.2" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
   <div ng-app="app" ng-controller="ctrl">

 <select class="form-control" ng-model="distancetypeId" ng-options="Distance.id as Distance.value for Distance in data"></select>
<select class="form-control" ng-model="ConverttypeId" ng-options="Convert.id as Convert.type for Convert in convertor" ng-change="convertToMeter()"></select>

</div>
  </body>

</html>

AngularJS

angular.module("app",[])
.controller("ctrl",function($scope){


    $scope.data = [{
    "id": 1,
    "value": 254
  },
  {
    "id": 2,
    "value": 100
  },
  {
    "id": 3,
    "value":250 
  },
  {
    "id": 4,
    "value":10 
  }]

    $scope.convertor = [{
    "id": 1,
    "type": "Meter"
  },
  {
    "id": 2,
    "type": "Yard"
  }];



 $scope.convertToMeter=function(ConverttypeId){
           if(ConverttypeId = 2){

    //this is logic that i want to do  (dropdown1values=value*1.0936;)

 }}

 });

2 个答案:

答案 0 :(得分:1)

因此,如果选择了 Yard ,那么您需要将距离值乘以1.0936,这样您就可以直接在ngOptions中执行此操作:

ng-options="Distance.id as (Distance.value * (1 + (ConverttypeId == '2') * 0.0936)) for Distance in data"

因此,如果Converttypeid等于'2'(这意味着选择了Yard选项),则距离乘以1 + (true * 0.0936),等于1.0936

如果不是距离乘以1 + (false * 0.0936)等于1,那么它仍然是相同的值。

Here is a plunker显示此内容

答案 1 :(得分:1)

您可以像这样修改ng-change功能

 $scope.convertToMeter=function( ){
    if($scope.ConverttypeId == 2){
    // document.getElementById("outputMeters").innerHTML=value/1.0936;
      for(var i=0; i<=$scope.data.length-1; i++){
        $scope.data[i].value =( $scope.data[i].value/ 0.9144 ).toFixed(2)
      }
    }else if($scope.ConverttypeId == 1){
      for(var i=0; i<=$scope.data.length-1; i++){
        $scope.data[i].value = parseInt($scope.data[i].value * 0.9144) 
      }
    }

 }

Demo