LS2
LS2

Reputation: 152

Angular JS Custom Directive not Working

I am trying to implement a directive in my app. Here is the following code.

    console.log("Directive loaded");
    angular.module("MyApp")
       .directive("datepickerLocal", function($rootScope) {
       console.log("datepickerLocal");
       return {
          restrict: 'E',
          require: 'ngModel',
          link: function(scope, elem, attr, ctrl) {
          ctrl.$parsers.unshift(function(viewValue) {
            var ngModelController = ctrls[0];
            ngModelController.$parsers.push(function (viewValue) {
               viewValue.setMinutes(viewValue.getMinutes() - viewValue.getTimezoneOffset());
               return viewValue.toISOString().substring(0, 10);
            });
         });
       }
    };
  }
 );

And here is the HTML text field.

    <input id="datepicker" type="text" data-ng-datepicker data-ng-options="datepickerOptions" ng-model="user.User.DateOfBirth" datepickerLocal>

I am able to see the first console message, but howeven the console message inside the directive is not appearing.

Feedback will be much appreciated.

Upvotes: 0

Views: 73

Answers (1)

ms87
ms87

Reputation: 17492

2 things:

  1. Your'e restricting the directive to E, saying it should be defined as an element, but you're using it an an attribute. Either change restrict: "E" to A or EA, or use it like:

  1. Directives need to be camel cased when used in html, that is:

datepickerLocal is wrong, should be : datepicker-local

Upvotes: 1

Related Questions