简体   繁体   English

在 angularJs 控制器和 TypeScript 控制器中使用 TypeScript 服务

[英]using TypeScript service in both angularJs Controllers and TypeScript Controllers

After converting my AngularJS Service to TypeScript using the word "export", then makes it available for import in a TypeScript controller...使用“导出”一词将我的 AngularJS 服务转换为 TypeScript 后,然后使其可用于在 TypeScript 控制器中导入...

, but how can i still injecting it ,with old controllers? ,但是我怎么还能用旧控制器注入它呢?

Explaining in Detail , I am rewriting my controllers to services on Typescript to be prepared for a future migration of angularjs to angular 2.x , Here is a service "equipmentAreaStorageT" using TypeScript...详细解释一下我正在将我的控制器重写为 Typescript 上的服务,以准备将来将 angularjs 迁移到 angular 2.x ,这是使用 TypeScript 的服务“equipmentAreaStorageT”...

     <reference path="../../Scripts/typings/angularjs/angular.d.ts" /> 
     import { UrlService } from '../../Scripts/app/services/urlService'; 
     export class EquipmentAreaStorageT { 

    keyPrefix = "";
    api = "";
    index =""; 
    constructor(private isl3UrlService: UrlService) {

        var _this = this; 
        this.keyPrefix = "EquipmentAreaStorageT";
        this.api = "api/equipmentAreaApi/";
        this.index = "EquipmentArea";        
    }
    getIndexUrl() {
        return this.isl3UrlService.getPath + this.index;
    }    
}
angular.module('main').service('equipmentAreaStorageT', EquipmentAreaStorageT);

and I know using the Import keyword should be enough for injecting this into a TypeScriptController我知道使用 Import 关键字应该足以将其注入 TypeScriptController

import { EquipmentAreaStorageT } from '../EquipmentArea/EquipmentAreaStorageT'; 
export class EquipmentAreaControllerT {
    $inject = ["$scope"];

    entity = {};
    isOpen = false; 
    gridOptions = {}; 
    isl3SelectionService = {};
    isl3DialogService = {}; 
    constructor($scope,private isl3EquipmentAreaStorage: EquipmentAreaStorageT) {    
        var _this = this;

        var ctrl = $controller("baseEditOnGridController", { $scope: this, storage: isl3EquipmentAreaStorage});        

        ctrl.loadRecords();
    }
}
angular.module('main').controller('EquipmentAreaControllerT', EquipmentAreaControllerT);

but i dont want to transform all my Controllers to Typescript.. not at the moment, so how can i use this new TypeScript service in my old .js controller.但我不想将我所有的控制器转换为 Typescript .. 目前还没有,那么我如何在我的旧 .js 控制器中使用这个新的 TypeScript 服务。 i have tryed this for example..例如,我已经尝试过这个..

appRoot.controller("EquipmentAreaController", [
    "$scope", "equipmentAreaStorageT"
    function ($scope, equipmentAreaStorageT) {

        $scope.entity = {};
        $scope.isOpen = false;

        $controller("baseEditOnGridController", { $scope: $scope, storage: equipmentAreaStorageT});

        $scope.loadRecords();

    }
]);

but i get always this error.但我总是收到这个错误。 Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });

im working with visual studio and added the Types for working with typescript.我正在使用visual studio并添加了用于打字稿的类型。

If you just import service in a controller - it wouldn't be the same as injecting.如果您只是在控制器中导入服务 - 它与注入不同。 You can use imported value only for typescript typings.您只能将导入的值用于打字稿类型。

You should properly set up your builder (webpack, gulp - whatever you use) to work with typescript.您应该正确设置您的构建器(webpack、gulp - 无论您使用什么)以使用打字稿。 Everything still will be the same as with javascript.一切都将与 javascript 相同。 You still need to register service in some module and inject in another place.您仍然需要在某个模块中注册服务并在另一个地方注入。 If you are using AngularJs 1.5+ consider using components.如果您使用的是 AngularJs 1.5+,请考虑使用组件。 Also look at ng-annotate (or it's babel plugin to simplify injection)也看看 ng-annotate (或者它是简化注入的 babel插件

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

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