In my Angular app, I defined a custom slider directive that wraps <input type="range">. My custom directive supports ngModelto bind the value of sliders to a variable. A custom directive also requires an attribute fraction-size. It calculates the value and then uses the result to set the value of the stepwrapped one <input>.
I see an error when I combine these two functions - ngModeland the value of the associated attribute. They are executed in the wrong order.
Here is a demo:
angular.module('HelloApp', []);
angular.module('HelloApp').directive('customSlider', function() {
var tpl = "2 <input type='range' min='2' max='3' step='{{stepSize}}' ng-model='theNum' /> 3";
return {
restrict: 'E',
template: tpl,
require: 'ngModel',
scope: {
fractionSize: '='
},
link: function(scope, element, attrs, ngModelCtrl) {
scope.stepSize = 1 / scope.fractionSize;
scope.$watch('theNum', function(newValue, oldValue) {
ngModelCtrl.$setViewValue(newValue);
});
ngModelCtrl.$render = function() {
scope.theNum = ngModelCtrl.$viewValue;
};
}
};
});
angular.module('HelloApp').controller('HelloController', function($scope) {
$scope.someNumber = 2.5;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<div ng-app="HelloApp" ng-controller="HelloController">
<h3>Custom slider</h3>
<custom-slider ng-model="someNumber" fraction-size="10"></custom-slider>
<h3>View/edit the slider’s value</h3>
<input ng-model="someNumber"></input>
</div>
Run codeHide result, 2.5. ( 3). 2.5, , , .
, - , . , , <input> s step undefined 1. ngModel 2.5 - step 1, input 3. , step 0.1 - , .
, step ngModel value?
. . step value , .
,
, step , . , , <input> :
<input type="range" min="2" max="3" step="0.1" ng-model="someNumber">
, value , . customSlider, step .
, ,
step ng-model .compile link. stepSize compile, scope .link -. scope.stepSize pre post, -.scope.$digest() scope.stepSize Error: [$rootScope:inprog] $digest already in progress.- , , .
step s , raw {{}} -. , step - , .