peanut
peanut

Reputation: 283

Angular Directive - NgModelCtrl Parser Issue

I have a directive to show a symbol depending on the value of a field. This is attached to a field as follows:

<input type="text" placeholder="" class="text-input" ng-class="example_class" ng-model="exmaple-model" my-directive />

And a directive as follows:

module.directive("myDirective", function () {
    return {
        require: "?ngModel",

        link: function (scope, element, attrs, ngModelCtrl) {
            /*breakpoints show the line above and below are reached*/
            ngModelCtrl.$parsers.push(function (val) {
                /* call to function to show symbol, this line is never reached */
            }

The code is reaching the ngModelCtrl line, but reaches no further. Debugging has shown that while the ngModelCtrl looks to be built correctly (has functions and values, etc.) the $parsers is empty - the length is 0, the functions that should be there aren't.

Looking at the Chrome inspector yields no errors. Are there any reasons for the $parsers to be blank, or is there a way to debug the directive any further?

Upvotes: 0

Views: 65

Answers (2)

Aman Bachas
Aman Bachas

Reputation: 274

Your parser is showing 0 length, try to return some value from the parse method and use it as follows:

function parse(value) {
  if (value) {
    return value.toLowerCase();
  }
}
ngModelController.$parsers.push(parse);

Upvotes: 2

Billy
Billy

Reputation: 35

Check out https://alexperry.io/angularjs/2014/12/10/parsers-and-formatters-angular.html

Have you tried putting your logic directly where you have "call to function to show symbol" vs calling the function?

Upvotes: 0

Related Questions