-
-
Save skelet00r/b20160fced5963a338ab to your computer and use it in GitHub Desktop.
<html> | |
<body> | |
//Note the value attribute rather than ng-model | |
<spin value="someScopeValue" min="1" max="{{someOtherScopeValue}}" step="2" /> | |
$scope.someScopeValue = 2; | |
$scope.someOtherScopeValue = 20; | |
</body> | |
</html> |
MIT License | |
Copyright (c) [2016] [James Delibas] | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
app.directive('spin', function() { | |
return { | |
templateUrl: 'spinner.html', | |
scope: { | |
'value' : "=" | |
}, | |
restrict: 'E', | |
link: function(scope, element, attrs, ngModel) { | |
var min,max,step,value,input,initial; | |
element = angular.element(element); | |
if(typeof attrs === 'undefined'){ | |
throw new Error('Spin.js attributes missing'); | |
} else { | |
min = typeof attrs.min !== 'undefined' ? attrs.min : 0; | |
max = typeof attrs.max !== 'undefined' ? attrs.max : 999; | |
step = typeof attrs.step !== 'undefined' ? attrs.step : 1; | |
initial = parseInt(scope.value); | |
input = $("input[name='spin']",element); | |
input.TouchSpin({ | |
min: min, | |
max: max, | |
step: parseInt(step), | |
initval: initial, | |
forcestepdivisibility : 'none', | |
booster : false | |
}); | |
input.on('change', function(e){ | |
scope.value = input.val(); | |
//hack | |
if(!scope.$$phase) { | |
scope.$apply(); | |
} | |
}); | |
} | |
} | |
}; | |
}); |
<input type="text" name="spin" class="form-control spinner-input"> |
If I try yours, it says "input.val() is not a function". Where is it defined?
scope.ngModel == 'undefined'
@Asscobara I guess we have the same question.. So, I make a little change in directive:
`
.directive('spin', function() {
return {
templateUrl: '../app/lib/bootstrap-touchspin/templates/touchspinner.html',
require: 'ngModel',
scope: {
ngChange: '&',
ngKeyup: '&',
value: '@'
},
transclude: true,
restrict: 'E',
link: function(scope, element, attrs, ctrl) {
var min, max, step, input, initial, value;
element = angular.element(element);
if (typeof attrs === 'undefined') {
throw new Error('Spin.js attributes missing');
} else {
min = typeof attrs.min !== 'undefined' ? attrs.min : 0;
max = typeof attrs.max !== 'undefined' ? attrs.max : 999999999;
step = typeof attrs.step !== 'undefined' ? attrs.step : 1;
value = typeof attrs.value !== 'undefined' ? attrs.value : 0;
//initial = parseInt(scope.ngModel);
input = $("input[name='spin']", element);
input.TouchSpin({
min: min,
max: max,
step: parseInt(step),
initval: value,
forcestepdivisibility: 'none'
});
input.on('change', function(e) {
ctrl.$setViewValue(input.val());
ctrl.$render();
});
input.on('keyup', function(e) {
ctrl.$setViewValue(input.val());
ctrl.$render();
});
}
}
};
})
`
using the directive:
<spin id="qtd" value="{{forno.quantidadeLinha}}" ng-model="forno.quantidadeLinha" ng-change="qtdLinhas(forno.quantidadeLinha)" ng-keyup="qtdLinhas(forno.quantidadeLinha)" />
@lshunran
Better decision is add the model to scope without any other modifications:
scope: { ngModel : '=', ngChange: '&', ngKeyup: '&' },
Voila :)
angular.module('angular-spin', [])
.directive('spin', function() {
return {
templateUrl: contextPath+'/js/angularjs/directives/spinner.html',
require: 'ngModel',
scope: {
ngModel: '=',
ngChange: '&',
ngKeyup: '&'
},
transclude: true,
restrict: 'EA',
link: function(scope, element, attrs, ctrl) {
var min,max,step,input,initial;
element = angular.element(element);
if(typeof attrs === 'undefined'){
throw new Error('Spin.js attributes missing');
} else {
min = typeof attrs.min !== 'undefined' ? attrs.min : 0;
max = typeof attrs.max !== 'undefined' ? attrs.max : 999;
step = typeof attrs.step !== 'undefined' ? attrs.step : 1;
value = typeof attrs.value !== 'undefined' ? attrs.value : 1000;
input = $("input[name='spin']",element);
input.TouchSpin({
min: min,
max: max,
step:parseInt(step),
initval: value,
forcestepdivisibility:'none'
});
input.on('change', function(e){
ctrl.$setViewValue(input.val());
ctrl.$render();
});
input.on('keyup', function(e){
ctrl.$setViewValue(input.val());
ctrl.$render();
});
scope.$watch('ngModel', function(newValue, oldValue) {
if (!angular.isUndefined(newValue)) {
input.val(newValue);
}
});
}
}
};
});
Esse é meu modelo, funciona perfeitamente. Mas não pode colocar dentro de um md-input-container
Thank you 👍