简体   繁体   中英

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 ..

I also encountered such problems and just made

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

OR

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

to avoid the problem.

为了完整起见,我还发现了另一种使用预构建指令的方法,可以为此操作插入该指令typescript-key-enter-directive

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM