Tuhin
Tuhin

Reputation: 3373

Directive not working in Angularjs, Not getting called

I'm trying to write formatter and parser through a directive.

angular.module('myModule').directive('toDate', function(){
        debugger; //Debugger Not Coming Here
        return{
            require: 'ngModel',
            link: function(scope, element, attr, ngModel){
                debugger; //Debugger Not Coming Here Too
                ngModel.$formatters.push(function(value){
                        return new Date(value);
                });

                ngModel.$parsers.push(function(value){
                    if(typeof value === 'object'){
                        return value.getTime();
                    }
                });

            }
        };
    });

using it in view :

 <td><input type="text"  ng-model="item.s_modified_on" toDate /></td>

It is not working.

Upvotes: 0

Views: 448

Answers (1)

K K
K K

Reputation: 18099

Use

 <td><input type="text"  ng-model="item.s_modified_on" to-date /></td>

instead of

 <td><input type="text"  ng-model="item.s_modified_on" toDate /></td>

Directives have camel cased names such as ngBind. The directive can be invoked by translating the camel case name into snake case with these special characters :, -, or _. Optionally the directive can be prefixed with x-, or data- to make it HTML validator compliant.

Reference: https://docs.angularjs.org/guide/directive

Upvotes: 3

Related Questions