【问题标题】:Can't inject service to controller angularJs + CordovaJs (TypeScript)无法向控制器 angularJs + CordovaJs (TypeScript) 注入服务
【发布时间】:2015-06-27 21:07:00
【问题描述】:

我有应用 angularJs + CordovaJs + TypeScript 我尝试向控制器注入服务

module EC {
export class Bootstrap {
constructor($scope) {
  angular.module('ec', ['ec.services', 'ec.controllers']);
  angular.module('ec.services', []);
  angular.module('ec.controllers', []);

  //Constructor for class EC.Services.DataProxyService called
  angular.module('ec.services').factory("DataProxyService", EC.Services.DataProxyService);

  //Version 1
  // It is not working. Alert doesn't call
  angular.module('ec.controllers').controller("HomeController", ['$scope', 'DataProxyService', EC.Controllers.HomeController]);

  //Version 2
  //If I change previous on that
  //it work's but I don't have access to DataProxyService
  //angular.module('ec.controllers').controller("HomeController", ['$scope', EC.Controllers.HomeController]);
    }
  }
}

在单独的 javascript 文件 (index.js) 中调用此类

   var init = new EC.Bootstrap();

我很困惑,因为它必须工作,但它不能在 android 设备(手机)和 android 模拟器上工作

我尝试使用很多变体,但它们也不起作用,无论如何这种情况必须有效

我在调试此代码时遇到问题,我无法得到错误,但简单的角片不适用于版本 1 并且适用于版本 2

UPD:2 我使用 gulp 所以我得到下一个文件:

    /// <reference path="references.ts" />
var EC;
(function (EC) {
    var Bootstrap = (function () {
        function Bootstrap($scope) {
            angular.module('ec', ['ec.services', 'ec.controllers']);
            angular.module('ec.services', []);
            angular.module('ec.controllers', []);
            //Constructor for class EC.Services.DataProxyService called
            angular.module('ec.services').factory("DataProxyService", EC.Services.DataProxyService);
            //Version 1
            // It is not working. Alert doesn't call
            angular.module('ec.controllers').controller("HomeController", ['$scope', 'DataProxyService', EC.Controllers.HomeController]);
            //Version 2
            //If I change previous on that
            //it work's but I don't have access to DataProxyService
            //angular.module('ec.controllers').controller("HomeController", ['$scope', EC.Controllers.HomeController]);
        }
        return Bootstrap;
    })();
    EC.Bootstrap = Bootstrap;
})(EC || (EC = {}));
//# sourceMappingURL=boot.js.map
//// <reference path="../scripts/typings/tsd.d.ts" />
//// <reference path="../../typings/kendo.all.d.ts" />
//// <reference path="../../typings/api.d.ts" />
//# sourceMappingURL=references.js.map
/// <reference path="../references.ts" />
var EC;
(function (EC) {
    var Controllers;
    (function (Controllers) {
        var HomeController = (function () {
            /*@ngInject*/
            function HomeController($scope, dataProxyService) {
                this.name = "Smith";
                alert('HomeController');
                alert($scope);
                alert(dataProxyService);
                $scope.vm = this;
            }
            return HomeController;
        })();
        Controllers.HomeController = HomeController;
    })(Controllers = EC.Controllers || (EC.Controllers = {}));
})(EC || (EC = {}));
//# sourceMappingURL=home-controller.js.map
/// <reference path="../references.ts" />
var EC;
(function (EC) {
    var Services;
    (function (Services) {
        var DataProxyService = (function () {
            /*@ngInject*/
            function DataProxyService() {
                alert('DataProxyService');
            }
            return DataProxyService;
        })();
        Services.DataProxyService = DataProxyService;
    })(Services = EC.Services || (EC.Services = {}));
})(EC || (EC = {}));
//# sourceMappingURL=dataProxyService.js.map

【问题讨论】:

  • 你在哪里定义EC.Services.DataProxyService
  • 在与其他文件的一个文件中/我添加到“gulp”之后得到的代码的开始消息示例
  • 你在哪里定义 ec.services 模块。例如angular.module('ec.services', [])
  • 对不起,我用代码做了很多实验,丢失了重要的字符串,我更新帖子并修复了代码

标签: javascript android angularjs cordova typescript


【解决方案1】:

试试这个

module EC {
  export class Bootstrap {
constructor($scope) {
  angular.module('EC', []);
  angular.module('Services', []);

  //Constructor for class EC.Services.DataProxyService called
  angular.module('Services').service("DataProxyService", [Services.DataProxyService]);

  //Version 1
  // It is not working. Alert doesn't call
  angular.module('EC', ['Services']).controller("HomeController", ['$scope', 'DataProxyService', Controllers.HomeController]);

  //Version 2
  //If I change previous on that
  //it work's but I don't have access to DataProxyService
  //angular.module('ec.controllers').controller("HomeController", ['$scope', EC.Controllers.HomeController]);
}  }}

在 *.ts 文件中将模块名称更改为简单的名称,如“服务”和“控制器”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-02
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 2019-06-23
    相关资源
    最近更新 更多