写一个角指令与打字稿类 [英] Writing an Angular directive with a TypeScript class

查看:91
本文介绍了写一个角指令与打字稿类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能只是试图结合太多新对我立刻概念,但是我想写使用打字稿类的定制角度指令。此刻,我不是做什么多大用处,只是一个POC。

I may just be attempting to combine too many "new-to-me" concepts at once, but I am trying to write a custom Angular directive using a TypeScript class. At the moment, I'm not trying to do anything terribly useful, just a POC.

我有一个打字稿文件看起来像这样:

I have a TypeScript file that looks like this:

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中:

It compiles to this in 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 = {}));

我的角度模块的样子(我甚至不知道我是否需要这样做):

My angular module looks like (I don't even know if I need to do this):

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

和我试图在我看来,使用它:

And I attempt to use it in my view:

<div app-stepper></div>

和得到这些错误:

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

为什么我的应用了解我的指令?

推荐答案

虽然这是不太相同的问题,这个答案包括哪些我试图做一个例子:<一href=\"http://stackoverflow.com/questions/30482138/how-can-i-define-my-controller-using-typescript/30506888#30506888\">How我可以用打字稿定义我的控制?

Though it is not quite the same question, this answer included an example of what I'm attempting to do: How can I define my controller using TypeScript?

我也跟着在Plnkr它引用,并找到了成功的范例:的http:// plnkr.co/edit/3XORgParE2v9d0OVg515?p=$p$pview

I followed the example in the Plnkr it referenced and found success: http://plnkr.co/edit/3XORgParE2v9d0OVg515?p=preview

我的最后打字稿指令如下:

My final TypeScript directive looks like:

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()]);
}

这篇关于写一个角指令与打字稿类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆