简体   繁体   English

AngularJS指令(TypeScript)

[英]AngularJS Directive (TypeScript)

I'm trying to figure out how to use directives in AngularJS with TypeScript. 我试图弄清楚如何在AngularJS和TypeScript中使用指令。 I have followed a tutorial, where in I followed every step accordingly.. but I ran in to some troubles. 我遵循了一个教程,在该教程中,我按照每个步骤进行了相应的操作..但是遇到了一些麻烦。 For some reason it's not displaying the content of my Directive. 由于某种原因,它没有显示我的指令的内容。

The directive itself: 指令本身:

module Directives {
    debugger;
    export interface IMyScope extends ng.IScope
    {
        name: string;
    }

    export function LeftMenu(): ng.IDirective {
        return {
            template: '<div>{{name}}</div>',
            scope: {},
            link: (scope: IMyScope) => {
                scope.name = 'Aaron';
            }
        };
    }
}

Now, I placed a debugger to check if my code even runs until it reaches the directive, and yes it does. 现在,我放置了一个调试器,以检查我的代码是否一直运行到到达指令为止,是的。 My chrome is debugging on the point where it is supposed to. 我的chrome正在调试它应该在的地方。

Registering all directives (even though I have only one atm): 注册所有指令(即使我只有一个atm):

/// <reference path="../reference.ts" />

angular.module('directives', []).directive(Directives)

My reference file: 我的参考档案:

/// <reference path="../wwwroot/libs/bower_components/definitelytyped/angularjs/angular.d.ts" />
/// <reference path="../wwwroot/libs/bower_components/definitelytyped/angularjs/angular-route.d.ts" />
/// <reference path="../wwwroot/libs/dx-libs/ts/dx.all.d.ts" />

/// <reference path="contollers/controllers.ts" />
/// <reference path="directives/directives.ts" />
/// <reference path="app.ts" />

Calling the directive like this: 像这样调用指令:

<div>
    <left-menu></left-menu>
</div>

There are no errors logged what so ever.. So, I hope someone has the solution to this problem. 迄今为止,没有记录任何错误。.因此,我希望有人可以解决此问题。

Thanks in advance! 提前致谢!

You have to register a directive with a name as first parameter. 您必须注册一个名称为第一个参数的指令。 This is how I would write your directive in combination with typescript: 这就是我将您的指令与打字稿结合起来写的方式:

export = class LeftMenu implements angular.IDirective {
    static ID = "leftMenu";

    public restrict = "AE";
    public template = '<div>{{name}}</div>';
    public scope = {};
    public link = (scope: IMyScope) => {
        scope.name = 'Aaron';
    }

    // to show how to handle injectables
    constructor(private somethingInjected){}

    static factory() {
        // to show how to handle injectables
        var directive = (somethingInjected) => new MyDirective(somethingInjected);
        directive.$inject = ["somethingInjected"];
        return directive;
    }
}

and to register it: 并注册:

import leftMenu = require("LeftMenu");

angular.module('directives', []).directive(leftMenu.ID, leftMenu.factory());

The debugger fires because you actually submit an object to .directive (typescript transpiles modules into objects by default). 之所以会启动调试器,是因为您实际上向.directive提交了一个对象(默认情况下,打字稿将模块转换为对象)。

You could try to initialize the directive either directly in the file declarating it: 您可以尝试直接在声明该文件的文件中初始化该指令:

angular
    .module("module.a")
    .directive("directive.a", (): angular.IDirective =>
    {
        return {
            restrict: "E",
            templateUrl: "template.a.html",
            controller: "controller.a",
            controllerAs: "$ctrl",
            link: () => {},
            bindToController: true
        };
    });

or simply the exported function 或者只是导出的功能

angular
    .module("module.a")
    .directive("directive.a", Directives.LeftMenu);

I am not aware of a typescript-module-aware version of the .directive function of angular, that registeres all directives of a module. 我不知道angular的.directive函数的可.directive打字稿模块的版本,该版本可注册模块的所有指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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