【问题标题】:AngularJS Filter: HexadecimalAngularJS 过滤器:十六进制
【发布时间】:2016-02-17 19:03:46
【问题描述】:

我正在尝试应用一个过滤器,该过滤器接受十进制输入并通过 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>

【问题讨论】:

  • 使其成为type="number" 将确保该值采用number 格式

标签: javascript angularjs filter hex


【解决方案1】:

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

return Number(input).toString(16);

【讨论】:

  • 我明白了。我需要添加 Number 构造函数。谢谢!
【解决方案2】:

两件事:

1.) 尝试将其插入您的 App.Module。 Angular.module("MyApp"['hexFilter']).

2.) angular.module('hexFilter', []).filter('hex', function() {

//代码

}

这些更改后它应该可以工作。有关更多文档,请参阅此处 https://docs.angularjs.org/tutorial/step_09

【讨论】:

    猜你喜欢
    • 2013-03-16
    • 2019-05-25
    • 2020-09-10
    • 2018-07-26
    • 2011-12-09
    • 2014-08-24
    • 2015-06-12
    • 2016-04-06
    • 2014-04-20
    相关资源
    最近更新 更多