Javascript 如何在typescript中的控制器中添加angularjs服务
我在很多网站上搜索过关于在typescript中向controller添加服务的内容。我曾经尝试过dfiffert网站上的一些示例,也尝试过表单堆栈溢出,但我仍然在努力。请你看看我的代码,里面有什么问题Javascript 如何在typescript中的控制器中添加angularjs服务,javascript,jquery,angularjs,asp.net-mvc,typescript,Javascript,Jquery,Angularjs,Asp.net Mvc,Typescript,我在很多网站上搜索过关于在typescript中向controller添加服务的内容。我曾经尝试过dfiffert网站上的一些示例,也尝试过表单堆栈溢出,但我仍然在努力。请你看看我的代码,里面有什么问题 module Application.Stores { export class StoreApplication { private sPresets: IStorePresets; constructor(presets: IStorePreset
module Application.Stores {
export class StoreApplication {
private sPresets: IStorePresets;
constructor(presets: IStorePresets) {
this.sPresets = presets;
}
init() {
var newStoresApp = angular.module('NewStoresApp', []);
this.dependency(newStoresApp);
newStoresApp.service('StoreCollectionService', ['$http', '$q', 'sPresets', ($http, $q, sPresets) => new Application.Stores.Services.StoreCollectionService($http, $q, sPresets)]);
// Working but without service
//newStoresApp.controller("CollectionCtrl", ['$scope', ($scope) => new Application.Stores.CollectionCtrl($scope)]);
// Not working
//newStoresApp.controller('CollectionCtrl', ['$scope', 'StoreCollectionService', Application.Stores.CollectionCtrl]);
//Not Working
//newStoresApp.controller("CollectionCtrl", ['$scope', 'StoreCollectionService', ($scope, StoreCollectionService) => new Application.Stores.CollectionCtrl($scope, StoreCollectionService)]);
newStoresApp.controller("CollectionCtrl", []); // How can I do it here?
}
private dependency(app: ng.IModule) {
app.factory("presets", () => {
return this.sPresets;
});
}
}
}
如何在scanrio中向控制器添加服务
我有以下包含js文件的顺序:
Html.RequiresJs("https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js");
Html.RequiresJs("~/Areas/Warehouse/Scripts/Stores/StoreCollectionService.js");
Html.RequiresJs("~/Areas/Warehouse/Scripts/Stores/StoreCollectionsCtrl.js");
Html.RequiresJs("~/Areas/Warehouse/Scripts/Stores/StoresApp.js");
控制器内部代码如下:
module Application.Stores {
import Services = Application.Stores.StoreCollectionService;
export class CollectionCtrl {
private $scope: any;
private storeCollectionService: StoreCollectionService;
constructor($scope: ng.IScope, storeCollectionService: StoreCollectionService) {
this.$scope = $scope;
this.init();
}
init(): void {
}
private GetAll() {
console.log("Got it all works.");
}
}
}
我已经试过这个例子$inject['service']。例如,您可以查看这些帮助链接来支持我的问题
您需要先向Angular注册服务(StoreCollectionService?)
app.service("StoreCollectionService", StoreCollectionService);
然后,正如您所说,您可以使用static$inject
注入服务
export class CollectionCtrl {
static $inject = ["$scope", "StoreCollectionService"]
constructor(private $scope: ng.IScope, private storeCollectionService: StoreCollectionService) {
this.init();
}
init(): void {
}
private GetAll() {
console.log("Got it all works.");
}
}
试着用这种方法让它工作
module Application.Stores {
export class StoreApplication {
constructor() {
}
}
var newStoresApp = angular.module("NewStoresApp", []);
newStoresApp.service('NewStoresServics', ['$http', NewStoresServics]);
newStoresApp.controller("NewStoresController", ['$scope', 'NewStoresServics', NewStoresController]);
}
这将使您的项目运行 您好,谢谢您的回答,您对回答很感兴趣。我已经注册了我的服务,你也可以在abopuve codwe中看到。newStoresApp.service('StoreCollectionService',['$http','$q','sPresets',($http,$q,sPresets)=>newapplication.Stores.Services.StoreCollectionService($http,$q,sPresets)]);如果你在我的问题中看到的话,我也用过这个技巧。“我已经试过这个例子$inject['service']