我有应用程序 angularJs + CordovaJs + TypeScript 我尝试向 Controller 注入(inject)服务
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 模拟器上工作
我尝试使用很多变体,但它们都不起作用,无论如何这种情况必须起作用
我在调试这段代码时遇到问题我无法得到错误但是简单的 Angular 部分不适用于版本 1 而适用于版本 2
更新: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
最佳答案
试试这个
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 文件中将模块名称更改为简单名称,如“Service”和“Controller”
关于javascript - 无法向 Controller angularJs + CordovaJs (TypeScript) 注入(inject)服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29759767/