微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript-用TypeScript类编写Angular指令

我可能只是试图一次组合太多“新手入门”概念,但是我试图使用TypeScript类编写自定义Angular指令.目前,我并没有做任何非常有用的事情,只是POC.

我有一个看起来像这样的TypeScript文件

module App {
    'use strict';

    export class appStepper {

        public link:(scope:angular.IScope, element: angular.IAugmentedJQuery, attrs: angular.IAttributes) => void;
        public template:string = '<div>0</div><button>-</button><button>+</button>';
        public scope = {};
        public restrict:string = 'EA';

        constructor(){ }

        public static Factory(){
            var directive = () =>
            { return new appStepper(); };
            return directive;
        }
    }

    angular.module('app').directive('appStepper', App.appStepper.Factory());
}

它在JavaScript中编译为:

(function(App) {
    'use strict';
    var appStepper = (function() {
        function appStepper() {
            this.template = '<div>0</div><button>-</button><button>+</button>';
            this.scope = {};
            this.restrict = 'EA';
        }
        appStepper.Factory = function() {
            var directive = function() {
                return new appStepper();
            };
            return directive;
        };
        return appStepper;
    })();
    App.appStepper = appStepper;
    angular.module('app').directive('appStepper', App.appStepper.Factory());
})(App || (App = {}));

我的角度模块看起来像(我什至不知道是否需要这样做):

angular.module('app',['appStepper'])

我尝试在我看来使用它:

<div app-stepper></div>

并得到以下错误

 Uncaught Error: [$injector:nomod] 
 Uncaught Error: [$injector:modulerr] 

为什么我的应用程序不知道我的指令?

解决方法:

尽管问题不完全相同,但此答案包含了我尝试执行的操作的示例:How can I define my controller using TypeScript?

我按照它引用的Plnkr中的示例进行操作,并发现成功:http://plnkr.co/edit/3XORgParE2v9d0OVg515?p=preview

我最后的TypeScript指令如下所示:

module App {
    'use strict';

    export class appStepper implements angular.IDirective {

        public link:(scope:angular.IScope, element: angular.IAugmentedJQuery, attrs: angular.IAttributes) => void;
        public template:string = '<div>0</div><button>-</button><button>+</button>';
        public scope = {};
        public restrict:string = 'EA';

        constructor(){ }

    }

    angular.module('app').directive('appStepper', [() => new App.appStepper()]);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐