gpt4 book ai didi

javascript - AngularJS 过滤器 : Hexadecimal

转载 作者:行者123 更新时间:2023-11-30 16:16:24 26 4
gpt4 key购买 nike

我正在尝试应用一个过滤器,该过滤器接受十进制输入并通过 AngularJS 过滤器输出十六进制。我不确定为什么不应用过滤器。我已经在控制台中使用示例数据检查了转换,例如10 --> 一个

有什么建议吗?谢谢!

var app = angular.module("myApp", []);

app.filter('hex', function () {

return function(input) {
return (input).toString(16);
};
});


app.controller('hexCtrl', ['$scope', 'hexFilter', function($scope, hexFilter) {
// Initialize initial values.
$scope.decimal = 0;

}]);
<div ng-app="myApp" ng-controller="hexCtrl">
<p>This input takes decimal: <input type="text" ng-model="decimal"></p>
<p>Returns a hexadecimal: {{ decimal | hex }}</p>
</div>

最佳答案

在进行转换之前将输入转换为数字:

return Number(input).toString(16);

关于javascript - AngularJS 过滤器 : Hexadecimal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35465446/

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