【问题标题】:AngularJS height counter directiveAngularJS 高度计数器指令
【发布时间】:2015-12-26 19:10:12
【问题描述】:

我想要达到的目标:
On -/+ 点击在高度计数器中添加或删除一英寸,并以英尺/英寸格式(6'4")显示高度。

当前问题:
当试图编辑Maikel Daloo Counter Directive 时,AngularJS 项目会显示一个空白页。

任何帮助和建议都会有所帮助,
谢谢。

原始来源: http://maikeldaloo.com/post/angularjs-counter-directive
Plnkr: http://plnkr.co/edit/8mzg4QHKDHwgfSiy1XqL?p=preview

指令:

fittingApp.directive('counter', function() {
    return {
        restrict: 'A',
        scope: { value: '=value' },
        template: '<a href="javascript:;" class="counter-minus" ng-click="minus()">-</a><a  href="javascript:;" class="counter-plus" ng-click="plus()">+</a><input type="text" class="counter-field" ng-model="value" ng-change="changed()" ng-readonly="readonly">',
        link: function( scope , element , attributes ) {
            if ( angular.isUndefined(scope.value) ) {
                throw "Missing the value attribute on the counter directive.";
            }
            var min = angular.isUndefined(attributes.min) ? null : parseInt(attributes.min);
            var max = angular.isUndefined(attributes.max) ? null : parseInt(attributes.max);
            var step = angular.isUndefined(attributes.step) ? 1 : parseInt(attributes.step);
            element.addClass('counter-container');
            scope.readonly = angular.isUndefined(attributes.editable) ? true : false;
            var setValue = function( val ) {
                scope.value = parseInt( val );
            };
            setValue( scope.value );
            scope.minus = function() {
                if ( min && (scope.value <= min || scope.value - step <= min) || min === 0 && scope.value < 1 ) {
                    setValue( min );
                    return false;
                }
                setValue( scope.value - step );
            };
            scope.plus = function() {
                if ( max && (scope.value >= max || scope.value + step >= max) ) {
                    setValue( max );
                    return false;
                }
                setValue( scope.value + step );
            };
            scope.changed = function() {
                if ( !scope.value ) setValue( 0 );
                if ( /[0-9]/.test(scope.value) ) {
                    setValue( scope.value );
                }
                else {
                    setValue( scope.min );
                }
                if ( min && (scope.value <= min || scope.value - step <= min) ) {
                    setValue( min );
                    return false;
                }
                if ( max && (scope.value >= max || scope.value + step >= max) ) {
                    setValue( max );
                    return false;
                }
                setValue( scope.value );
            };
        }
    };
});

控制器:

fittingApp.controller('statsCtrl', ['$scope', function($scope) {
    $scope.height = 3;
    $scope.chest = 3;
    $scope.waist = 3;
    $scope.hips = 3;
    $scope.thighs = 3;
}]);

HTML:

<h4>Height:</h4>
<div counter min="0" value="height"></div>
<h4>Chest:</h4>
<div counter min="0" value="chest"></div>
<h4>Waist:</h4>
<div counter min="0" value="waist"></div>
<h4>Hips:</h4>
<div counter min="0" value="hips"></div>
<h4>Thighs:</h4>
<div counter min="0" value="thighs"></div>

设计:



开发:


【问题讨论】:

  • 运行编辑后的指令时控制台是否有错误?
  • 现在我已恢复为 Maikel Daloo 默认指令,身高、胸围、腰围、臀围和大腿再次显示,您可以单击 -/+ 来添加和删除数字,但身高不会像英尺和英寸一样工作......
  • 创建一个 fiddle 将有助于调试更多

标签: html angularjs angularjs-directive angularjs-controller


【解决方案1】:

我认为将计数器的值保持为一个完整的数字,然后当您以英尺/英寸为单位显示该值时,您应该使用过滤器来执行转换。

类似于您在 Angular 中处理货币的方式,您将拥有如下内容: {{总计 |货币}}。

从技术上讲,您可以拥有与上述类似的东西。 https://scotch.io/tutorials/building-custom-angularjs-filters

更新:

看看这个文件 - 我在每个文件中都添加了注释,告诉你我添加了哪些新内容。

http://plnkr.co/edit/YueSevu6YWI1RphoY7h3?p=preview

- Bumped the steps to 10, added a minimum of 100
- Added an inches filter (basic one)
- Set the counter fields as hidden
- Placed value outside of the counter elements.

【讨论】:

  • 谢谢 Maikel,我非常感谢您提供的所有额外意见和建议。我很快就一起模拟了一个 Plnkr,如果我正确地实现了所有内容,您能否查看并告诉我! plnkr.co/edit/I1s24nojfkuHAw6FSzEF?p=preview
  • 看起来不错 :) 它正在完成这项工作。不要忘记在按钮上添加user-select: none,以便在您快速单击时不允许选择。
猜你喜欢
  • 2015-09-01
  • 2015-03-09
  • 1970-01-01
  • 2016-12-07
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 1970-01-01
  • 2015-10-09
相关资源
最近更新 更多