1

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.

8
  • i guess you may try ng-minlength or ng-maxlength.. Commented Jul 10, 2017 at 4:55
  • Is your fillsiebelloandetails(SiebelLoanDetails) being called when page loads? Commented Jul 10, 2017 at 4:56
  • Yes. On page load fillsiebelloandetails(SiebelLoanDetails) i am calling. Commented Jul 10, 2017 at 4:56
  • Hi Vivz. May i know why this is not working on page load? Commented Jul 10, 2017 at 5:19
  • Post more code, from the above code it is unclear as to what is causing the issue. Commented Jul 10, 2017 at 5:25

2 Answers 2

1

You need watch rangeNumber changes in directive

    link: function (scope, elem, attrs, ctrl) {
        if (!ctrl) return;

        scope.$watch(attrs.rangeNumber, function(){
            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;
            });

        })



    }
Sign up to request clarification or add additional context in comments.

2 Comments

For example if var a=10 and var b=20 then how can i write $scope.range=?
$scope.range = a + ',' + b
0

When you define a variable within function its scope is treated as functional(local) scope so the issue has occurred.You should declare the variable at the directive level scope.For this, you can try as follows.

  1. Declare the variable outside the function.
  2. Then assign the value from inside the function.

Eg.

......

    var vm=this;
    vm.range=[];   //declare variable outside the function 
    function fillsiebelloandetails(SiebelLoanDetails)
    {
      //some http ajax call and get values and assign it ti range.
      vm.range = '1,7';
    } 

I think i might be helpful for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.