Answer the question
In order to leave comments, you need to log in
How to emulate the spin buttons action of the input [type="number] tag?
Hello!
We have something like this input:
<input type="number"
id="{{ variable.opt.name }}"
name = "{{ variable.opt.name }}"
min = "{{ variable.opt.min }}"
max = "{{ variable.opt.max }}"
step = "{{ variable.opt.step }}"
ng-model = "variable.opt.value">
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
}
input[type='number'] {
-moz-appearance: textfield;
}
setTimeout(function () {
angular.element('input[type="number"]').each(function () {
var t = jQuery(this);
t.wrap('<div class="sb"></div>');
t
.before('<div class="sb-arrow sb-up"><i class="fl-up-arrow"></i></div>')
.after('<div class="sb-arrow sb-down"><i class="fl-down-arrow-2"></i></div>');
});
}, 100);
angular.element('body').on('click', '.sb-arrow', function () {
var t = jQuery(this),
input = t.parent().children('input'),
step = (input.attr('step')) ? Number(input.attr('step')) : 1;
if (t.hasClass('sb-up')) {
input.val(Number(input.val()) + step);
console.log('click');
} else if (t.hasClass('sb-down')) {
input.val(Number(input.val()) - step);
}
});
angular.element('body').on('click', '.sb-arrow', function () {
var t = jQuery(this),
input = t.parent().children('input'),
step = (input.attr('step')) ? Number(input.attr('step')) : 1;
if (t.hasClass('sb-up')) {
var e = new jQuery.Event("keyup", { keyCode: 38 } );
input.trigger(e)
} else if (t.hasClass('sb-down')) {
input.val(Number(input.val()) - step);
}
});
Answer the question
In order to leave comments, you need to log in
Everything was solved by using the directive.
The code turned out something like this:
sb-input.js
(function () { 'use strict';
var sbInput = function () { return {
restrict: 'E',
scope: {
variable: '='
},
templateUrl: '/templates/sb-input.html',
link: function(scope) {
console.log(scope);
scope.up = function () { scope.variable.opt.value += scope.variable.opt.step; };
scope.down = function () { scope.variable.opt.value -= scope.variable.opt.step; };
}
};};
angular
.module('myApp')
.directive('sbInput', sbInput);
})();
<div class="sb">
<div class="sb-arrow sb-up" ng-click="up()"><i class="fl-up-arrow"></i></div>
<input type="number" min="{{ variable.opt.min }}" max="{{ variable.opt.max }}" step="{{ variable.opt.step }}" ng-model="variable.opt.value">
<div class="sb-arrow sb-down" ng-click="down()"><i class="fl-down-arrow-2"></i></div>
</div>
<sb-input variable="variable"></sb-input>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question