Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Angularjs中将计数器值显示为2位数字?_Javascript_Angularjs_Data Binding_Angularjs Directive_Angularjs Filter - Fatal编程技术网

Javascript 如何在Angularjs中将计数器值显示为2位数字?

Javascript 如何在Angularjs中将计数器值显示为2位数字?,javascript,angularjs,data-binding,angularjs-directive,angularjs-filter,Javascript,Angularjs,Data Binding,Angularjs Directive,Angularjs Filter,我正在使用angularjs定时器构建倒计时显示 timer指令以整数形式返回小时、分钟和秒的值。 是指,1,2,3,。。。。但我想把这些值显示为两位数。 比如,01,02,03,04 是否有任何筛选器或指令来完成此操作?我想创建一个简单的自定义 过滤器用于格式化显示给用户的数据 用法 {{value | counterValue}} <li ng-repeat="item in array"> <digit content="item"></digit&

我正在使用angularjs定时器构建倒计时显示

timer指令以整数形式返回小时、分钟和秒的值。 是指,1,2,3,。。。。但我想把这些值显示为两位数。 比如,01,02,03,04


是否有任何筛选器或指令来完成此操作?

我想创建一个简单的自定义

过滤器用于格式化显示给用户的数据

用法

{{value | counterValue}}
<li ng-repeat="item in array">
    <digit content="item"></digit>
</li>
.directive('digit', function () {
    return {
        restrict: 'E',
        scope: {
            content: '='
        },
        link: function (scope) {
            if (scope.content < 10) {
                scope.zero = "0";
            } else {
                scope.zero = "";
            }
        },
        transclude: true,
        template: '{{zero}}{{content}}'
    };
});
过滤器

app.filter('counterValue', function(){
   return function(value){
     value = parseInt(value);
      if(!isNaN(value) && value >= 0 && value < 10)
         return "0"+ valueInt;
      return "";
   }
})
app.filter('counterValue',function(){
返回函数(值){
value=parseInt(值);
如果(!isNaN(值)&&value>=0&&value<10)
返回“0”+valueInt;
返回“”;
}
})

过滤器的替代方案是自定义指令:

用法

{{value | counterValue}}
<li ng-repeat="item in array">
    <digit content="item"></digit>
</li>
.directive('digit', function () {
    return {
        restrict: 'E',
        scope: {
            content: '='
        },
        link: function (scope) {
            if (scope.content < 10) {
                scope.zero = "0";
            } else {
                scope.zero = "";
            }
        },
        transclude: true,
        template: '{{zero}}{{content}}'
    };
});
  • 指令

    {{value | counterValue}}
    
    <li ng-repeat="item in array">
        <digit content="item"></digit>
    </li>
    
    .directive('digit', function () {
        return {
            restrict: 'E',
            scope: {
                content: '='
            },
            link: function (scope) {
                if (scope.content < 10) {
                    scope.zero = "0";
                } else {
                    scope.zero = "";
                }
            },
            transclude: true,
            template: '{{zero}}{{content}}'
        };
    });
    
    指令('digit',函数(){ 返回{ 限制:'E', 范围:{ 内容:'=' }, 链接:功能(范围){ 如果(范围内容<10){ scope.zero=“0”; }否则{ scope.zero=“”; } }, 是的, 模板:“{zero}}{{content}” }; });