【发布时间】: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