https://github.com/bree7e/lesson-services
https://github.com/bree7e/lesson-services
Last synced: 26 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/bree7e/lesson-services
- Owner: bree7e
- Created: 2018-11-22T17:38:02.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-11-23T09:48:58.000Z (over 7 years ago)
- Last Synced: 2025-02-22T03:43:14.209Z (over 1 year ago)
- Language: TypeScript
- Size: 105 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Сервисы и внедрение зависимостей
```bash
ng g service student
```
Добавить метод в `src\app\student.service.ts`
```ts
@Injectable({
providedIn: 'root',
})
export class StudentService {
constructor() {}
factorial(n): number {
return n !== 1 ? n * this.factorial(n - 1) : 1;
}
}
```
Использовать сервис в компоненте `src\app\app.component.ts`
```ts
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
title = 'lesson4';
constructor(private _studentService: StudentService) {}
ngOnInit() {
const a = this._studentService.factorial(12);
console.log(a);
}
}
```