我正在尝试获取函数转换的结果并将其显示在页面上
这是我的功能:
$scope.convert = function(myUnit, myUnit2, distance){
var result = 0;
if(myUnit === "Kilometer"){
if(myUnit2 === "Kilometer"){
result = distance;
}
else if(myUnit2 === "Meter"){
result = distance * 1000;
}
else if(myUnit2 === "Centimeter"){
result = distance * 100000;
}
else if(myUnit2 === "Millimeter"){
result = distance * 1000000;
}
else if(myUnit2 === "Mile"){
result = distance * 0.621371;
}
else if(myUnit2 === "Nautical Mile"){
result = distance * 0.539957;
}
}
}
该函数的调用方式如下:
<button class="button button-block button-balanced" ng-click="convert(myUnit.thisunit, myUnit2.thisunit2, formData.distance)">
Convert
</button>
这是我想显示结果的地方:
<label class="item item-input" style="border-style: solid;
border-color: black;">
<span class="input-label">Result:</span>
<p>DISPLAY RESULT HERE</p>
</label>
我很难显示结果...转换函数位于我的 Controller 内:
myApp.controller('mainController', ['$scope', function($scope)
按钮和我想要放置值的位置都在我的 Controller 的范围内。帮助将不胜感激!
最佳答案
将结果
分配给范围属性,例如
$scope.result = result;
并将其显示在您的模板中
<p>{{ result }}</p>
<小时/>
当我在这里时,让我向您介绍switch
声明
var result = 0;
if(myUnit === "Kilometer"){
switch (myUnit2) {
case 'Kilometer' :
result = distance;
break;
case 'Meter' :
result = distance * 1000;
break;
// you get the idea
}
}
$scope.result = result;
关于javascript - 使用angularjs获取函数返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28731985/