Twitter & GitHub: romanroe
var mod = angular.module("myApp", ["dependencyA", "dependencyB"]);
mod.controller("MyController", MyController);
function MyController() {
...
}
function MyController() {
this.name = "Max";
this.sayHello = function () {
alert("Hello " + this.name + "!");
};
}
<body ng-controller="MyController as myController">
<div>
Namen eingeben:
<input type="text" ng-model="myController.name">
</div>
<div>
Ihr Name: <span>{{myController.name}}</span>
</div>
<button ng-click="myController.sayHello()">Hallo!</button>
</body>
Probleme von Controllern
Daher Services
function MyService() {
this.sayHello = function(name) {
alert("Hello " + name + "!");
}
}
mod.service("MyService", MyService);
function MyService() {
var service = {};
service.sayHello = function(name) {
alert("Hello " + name + "!");
};
return service;
}
mod.factory("MyService", MyService);
mod.service("MyService", MyService);
mod.factory("MyService", MyService);
function MyController(MyService) {
this.name = "Max";
this.sayHello = function () {
MyService.sayHello(this.name);
};
}
<input type="text" ng-model="MODEL">
<button ng-click="FUNCTION()">Hallo!</button>
<person data="myController.personObj"/>
this.personObj = {firstname: "Max", lastname: "Mustermann"}
function PersonDirective() {
return {
restrict: "E",
scope: {
data: "="
},
controller: function ($scope) {
$scope.sayHello = function (person) {
alert("Hello " + person.firstname + "!");
}
},
template: "Hallo {{data.firstname}} {{data.lastname}}!" +
"<button ng-click='sayHello(data)'>Hello</button>"
}
}
mod.directive("person", PersonDirective);
angular.module("mod1").service("MyUtils", MyUtils);
angular.module("mod2").service("MyUtils", MyUtils);
angular.module("mod2").controller("MyController", MyController);
function MyController() {
this.help = function () {
...
};
this.name = "Max";
var privateList = [];
this.sayHello = function () {
...
};
this.unused = "";
}
return {
restrict: ...,
scope: ...,
controller: ...,
template: ...,
priority: ...,
templateUrl: ...,
templateNamespace: ...,
bindToController: ...,
require: ...,
compile: ...,
link: ...
}
function hello(name) {
alert("Hello " + name + "!");
}
function hello(name: any) {
alert("Hello " + name + "!");
}
function hello(name: string) {
alert("Hello " + name + "!");
}
var isDone: boolean = false;
var height: number = 6;
var name: string = "bob";
var list:number[] = [1, 2, 3];
enum Color {Red, Green, Blue}
var c: Color = Color.Green;
var b1: boolean = false;
var b2 = true;
b2 = b1;
var person = {
firstname: "Max",
lastname: "Mustermann"
};
interface Person {
firstname: string;
lastname: string;
}
var person: Person = {
firstname: "Max",
lastname: "Mustermann"
};
var person: Person = {
firstname: "Max",
lastname: "Mustermann"
};
function hello(person: Person) {
alert("Hello " + person.firstname + "!");
}
interface Person {
firstname: string;
lastname: string;
}
var person: Person = {
firstname: "Max",
lastnme: "Mustermann" // TYPO
};
class Person {
firstname: string;
lastname: string;
}
var person: Person = {
firstname: "Max",
lastname: "Mustermann"
};
class Person {
firstname: string;
lastname: string;
sayHello() {
alert(this.firstname);
}
}
var person: Person = {
firstname: "Max",
lastname: "Mustermann"
};
class Person {
firstname: string;
lastname: string;
constructor(firstname: string, lastname: string) {
this.firstname = firstname;
this.lastname = lastname;
}
sayHello() {
alert(this.firstname);
}
}
var person: Person = new Person("Max", "Mustermann");
person.sayHello();
class Person {
constructor(public firstname: string, public lastname: string) {
}
sayHello() {
alert(this.firstname);
}
}
var person: Person = new Person("Max", "Mustermann");
person.sayHello();
interface Person {
name: string;
age?: number;
}
var p1: Person = { name: "Max", age: 40 };
var p1: Person = { name: "Max" };
var p1: Person = { name: "Max", age: "40" };
var p1: Person = { name: "Max", city: "Bonn" };
// Array
var list = [1, 2, 3];
list.aProperty = "foo";
var i = list[1];
// Function
var myFunction = function () {
};
myFunction.aProperty = "bar";
var y = myFunction();
interface StringList {
[index: number]: string;
aProperty: number;
}
function myFunction(list: StringList) {
var str = list[2];
list.aProperty = "foo";
}
interface Alerter {
(message: string): void;
aProperty: number;
}
function myFunction(alerter: Alerter) {
alerter("my message");
alerter.aProperty = "bar";
}
interface Options {
args: string|string[];
}
var o1: Options = { args: "a" };
var o2: Options = { args: ["a", "b", "c"] };
var o3: Options = {
args: 1
};
function useFirstArg(o: Options) {
var a = o.args;
if(typeof a === 'string') {
return a.substring(5);
} else {
return a[0].substring(5);
}
}
var counter = 0;
export class Utils {
static message(message: string) {
alert((counter++) + " " + message);
}
}
import {Utils} from "./utils";
Utils.message("test");
class TsController {
private counter = 0;
name: string;
constructor() {
this.name = "Max";
}
sayHello() {
alert((this.counter++) + " Hello from TS!");
}
}
mod.controller("TsController", TsController);
class TsService {
private counter = 0;
sayHello() {
alert((this.counter++) + " Hello from TsService!");
}
}
mod.service("tsService", TsService);
// Variante 1
angular.module("app").service("MyService", function (utils) {
...
});
// Variante 2
angular.module("app").service("MyService", ["utils", function (utils) {
...
}]);
function service(name: string, inj: Function|any[]) {
// ...
}
return {
restrict: "E",
scope: {
data: "="
},
controller: function ($rootScope, $scope) { /* ... */ },
template: "..."
}
interface IDirective {
controller?: any;
restrict?: string;
scope?: any;
template?: any;
// ...
}
export class MyService {
// ...
}
appMod.service("MyService", MyService);
interface JQueryStatic {
(selector: string, context?: Element|JQuery): JQuery;
(element: Element): JQuery;
(html: string, ownerDocument?: Document): JQuery;
// ...
}
declare var $: JQueryStatic;
function PersonDirective() {
return {
restrict: "E",
scope: {
data: "="
},
controller: function ($scope) {
$scope.sayHello = function (person) {
alert("Hello " + person.firstname + "!");
}
},
template: "Hallo {{data.firstname}} {{data.lastname}}!" +
"<button ng-click='sayHello(data)'>Hello</button>"
}
}
mod.directive("person", PersonDirective);
interface TsDirectiveScope extends ng.IScope {
data: Person
}
class TsDirective implements ng.IDirective {
restrict = 'E';
scope = { data: "=" };
template = "...";
controller($scope: TsDirectiveScope) {
console.log("Firstname: " + $scope.data.firstname);
}
}
mod.directive('tsDirective', () => new TsDirective());
$ tsd install angularjs --save
Roman Roelofsen
@romanroe