angular-cn/public/docs/_examples/dependency-injection/ts/app/user.service.ts

23 lines
445 B
TypeScript

// #docregion
import { Injectable } from '@angular/core';
export class User {
constructor(
public name: string,
public isAuthorized = false) { }
}
// Todo: get the user; don't 'new' it.
let alice = new User('Alice', true);
let bob = new User('Bob', false);
@Injectable()
export class UserService {
user = bob; // initial user is Bob
// swap users
getNewUser() {
return this.user = this.user === bob ? alice : bob;
}
}