I have a few issues with my typescript/angular-code. I'm new to typescript and angular (but I finished a few tutorials etc.) so i would like to know some "Best Practices".
The thing I want to know is how to pass the '$http' from angular to the class so I can use it there. I know how to write angular code and I know how to handle TypeScript ... but both combined, man, awful. Biggest struggle for me.
Please give me some good advices.
Here is my code (app.ts):
/// <reference path="../Scripts/typings/_references.d.ts" />
var theapp= angular.module('myapp', []);
class Aktion {
input: string;
private httpService: any;
constructor(message: string, $http: any) {
this.input = message;
this.httpService = $http;
}
act() {
console.log("act: " + this.input);
this.httpService.get('http://localhost:8080/Service/rest/webservice/GetActions/' + this.input + '').
success(function (data, status, headers, config) {
console.log(data);
}).error(function (data, status, headers, config) {
console.log(data);
});
}
connect() {
this.httpService.get('http://localhost:8080/Service/rest/webservice/GetConnections/' + + '').
success(function (data, status, headers, config) {
console.log(data);
}).error(function (data, status, headers, config) {
console.log(data);
});
}}}
theapp.controller('AktionsController', ['$http', Aktion]);
UPDATE 1:
<!DOCTYPE HTML>
<html lang="de" ng-app="myapp">
<head>
<meta charset="utf-8" />
<title>TEST</title>
<link rel="stylesheet" href="css/app.css" type="text/css" />
<link rel='stylesheet' href='http://fonts.googleapis.com/css?family=Abel' type='text/css' />
<script type="text/javascript" src="Scripts/jquery-2.1.3.min.js"></script>
<script type="text/javascript" src="Scripts/angular.js"></script>
<script src="ts/app.js"></script>
</head>
<body>
<content>
<div id="eingabe-container" ng-controller="AktionsController as aktionCtrl">
<h1>Test</h1>
<input id="eingabe" />
<button ng-click="aktionCtrl.act()">Abschicken</button>
</div>
</content>
</body>
</html>
I'm calling the act()-function via ng-click. Thought this is ok to do. Saw it very often in other code snippets. Any other suggestion?
This is just a test-app. I have to write a programm for my Bachelor-thesis and wanted to understand angular and typescript before writing the programm. So I made this Test-App.
Thanks to all your answeres. :)
$httpservice will be injected as your first parameter in theAktionconstructor. This means that$httpis actually inthis.input, andthis.httpServiceis undefined. I would suggest to remove the first parameter of your constructor. What is the goal you try to achieve with it?act()-functionin another way? Or, how do I pass the Input-value tothis.inputinclass Aktion?