I am developing a web application in angular js. I have textbox and directive. My textbox should take only values in certain range otherwise error message should be displayed. Below HTML code.
<input class="" type="text" name="rangeNumber" ng-attr-placeholder="{{ 'DownPayment' }}" ng-model="DownPayment" range-number="range">
<ul class="error-msgs">
<li ng-show="!form5.rangeNumber.$invalid && form5.rangeNumber.$dirty">Number not in range</li>
</ul>
Below is my directive.
app.directive('rangeNumber', function () {
return {
require: 'ngModel',
link: function (scope, elem, attrs, ctrl) {
if (!ctrl) return;
var range = scope.$eval(attrs.rangeNumber);
range = range.split(',').map(Number);
ctrl.$parsers.push(function (value) {
value = +value;
var isValidRange = !(value >= range[0] && value <= range[1]);
ctrl.$setValidity('range', isValidRange);
return value;
});
}
};
});
Below is my function where i am assigning range.
function fillsiebelloandetails(SiebelLoanDetails)
{
//some http ajax call and get values and assign it ti range.
$scope.range = '1,7';
}
Here I am facing problem if I assign $scope.range = '1,7'; inside fillsiebelloandetails it is not working. if I write $scope.range = '1,7'; outside function it works fine. May I know why this is happening? Any help would be appreciated. Thank you.