我想要实现的目标:
开 - / +单击从高度计数器添加或移除英寸,并以英尺/英寸格式显示高度(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>
答案 0 :(得分:0)
我认为将计数器的值保留为完整数字,然后当您以英尺/英寸显示值时,应使用过滤器执行转换。
与您在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.