Reputation: 41
I have a very simple webpage:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<title></title>
</head>
<body ng-app="nawApp">
<div ng-controller="StudentUse">
{{fullname}}
</div>
<script src="../Scripts/angular.js"></script>
<script src="../Scripts/angular-route.js"></script>
<script src="../Scripts/app.js"></script>
<script src="StudentUse.js"></script>
<script src="ServiceHttp.js"></script>
</body>
</html>
And I have my controller StundentUse.
/// <reference path="../scripts/app.ts" />
/// <reference path="ServiceHttp.ts" />
/// <reference path="../scripts/typescript/angular.d.ts" />
/// <reference path="Student.ts" />
module nawApp {
export interface IStundentScope extends ng.IScope {
returnData: string;
}
export class StudentUse {
static $inject = ['$scope', 'ServiceHttp'];
constructor($scope: IStundentScope, private serviceHttp: nawApp.ServiceHttp) {
$scope.returnData = serviceHttp.hello();
}
}
// StudentUse.$inject = ['$scope', 'ServiceHttp'];
nawApp.app.controller('StudentUse', StudentUse);
}
I have another file called ServiceHttp.ts, which is used by the upper code. The "ServiceHttp.ts" should be some kind of REST-Interface in the future, but up to now it simply returns a "Hello World" string:
/// <reference path="../scripts/app.ts" />
module nawApp {
export class ServiceHttp {
constructor() {}
hello(): string {
return "Hello world";
}
}
nawApp.app.controller('ServiceHttp', ServiceHttp);
}
If I launch my webpage, switch to the developer console, I get an error that saiys:
Error: [$injector:unpr] Unknown provider: serviceHttpProvider <- serviceHttp
I did't some investigation, but I cannot see the difference between examples that work and by solution with the error..... :-(
Any tips?
Thank You!
EDIT: This should be somehow the final code of the function in ServiceHttp.ts:
exampleFunction():string {
this.http.get(URL).success(function (data, status, headers, config) {
try {
return data;
}
catch (err) {
return err;
}
}).error(function (data, status, headers, config) {
return status;
});
return "";
}
Upvotes: 0
Views: 4407
Reputation: 4611
At first I want to suggest to include angular.d.ts in top
/// <reference path="../scripts/typescript/angular.d.ts" />
/// <reference path="../scripts/app.ts" />
/// <reference path="ServiceHttp.ts" />
/// <reference path="Student.ts" />
and for service you should create like this
module nawApp {
export class ServiceHttp {
private http: ng.IHttpService;
private q: ng.IQService;
private log: ng.ILogService;
constructor($http: ng.IHttpService,$q: ng.IQService,$log: ng.ILogService) {
this.http = $http;
this.q = $q;
this.log = $log;
}
exampleFunction(){
var defer = this.q.defer();
this.http.get('http://example.com').then(
(response: ng.IHttpPromiseCallbackArg<any>) => {
defer.resolve(response);
}
},
(err: ng.IHttpPromiseCallbackArg<any>) => {
defer.reject(err.data);
}
)
return defer.promise;
}
static Factory($http:ng.IHttpService, $q:ng.IQService,$log: ng.ILogService) {
return new ServiceHttp($http, $q, $log);
}
}
}
It's a service example. Then you need register your service into your app like this
nawApp.app.factory('ServiceHttp', nawApp.ServiceHttp.Factory);
somewhere in code, for example in controller, you should inject your service through and in controller function
controller constructor
private ServiceHttp:any;
constructor(ServiceHttp .... // other dependencies){
this.ServiceHttp = ServiceHttp;
}
in ctrl function
someCtrlFunction(){
this.ServiceHttp.exampleFunction().then(function(resp){
// success resolved with your response
})
}
Upvotes: 1