Search code examples
javascriptangularjstypescriptangularjs-directivewebstorm

AngularJS: Property 'ngEnter' does not exist on type 'IAttributes'


I'm using Typescript and migrating the DefinitelyTyped definitions for angular from 1.3 to 1.6.2 leads to the following error in my MainModule.ts:

Error:(14, 43) TS2339:Property 'ngEnter' does not exist on type 'IAttributes'.

The code that triggers the compiler error is defining the angularjs module:

angular.module('myApp', ['ngRoute', 'ui.bootstrap', 'ui.bootstrap.modal', 'smart-table'])
    .service('appService', AppService)
    .controller('MainCtrl', MainCtrl)
    .controller('uploadTSCtrl', UploadTSCtrl)
    .controller('inputCtrl', InputCtrl)
    .controller('reportCtrl', ReportCtrl)
    .directive('ngEnter', function () {
        return function (scope, element, attrs) {
            element.bind("keydown keypress", function (event) {
                if(event.which === 13) {
                    scope.$apply(function (){
                        scope.$eval(attrs.ngEnter); // <<<<<<<<<<<<< here
                    });
                    event.preventDefault();
                }
            });
        };
    })

I could not find a migration guide or anything like it from 1.x to 1.6.2 and don't know how to fix it ..


Solution

  • I also encountered such problems and just made

    scope.$eval(attrs['ngEnter']);
    

    OR

    scope.$eval((attrs as any).ngEnter);
    

    to avoid the problem.