AngularJS 高度计数器指令

AngularJS height counter directive

我要实现的目标:
在 -/+ 上单击从高度计数器添加或删除一英寸并以 foot/inch 格式 (6'4") 显示高度。

当前问题:
尝试编辑 Maikel Daloo 反指令时,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>

设计:



发展:


我认为将计数器的值保留为一个完整的数字,然后当您在 feet/inches 中显示该值时,您应该使用过滤器来执行转换。

类似于您在 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.