Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, OnInit, OnDestroy } from '@angular/core';
- import { Router, ActivatedRoute } from '@angular/router';
- import { NgForm } from '@angular/forms';
- import { CategoryprojectDataService} from '../../model/categoryproject-data.service';
- import { categoryproject } from '../../model/categoryproject';
- @Component({
- templateUrl: './categoryproject-form.component.html',
- })
- export class CategoryprojectformComponent implements OnInit, OnDestroy {
- private _mode = '';
- private _id:number;
- private _parameters:any;
- private _category:categoryproject;
- private _errorMessage:string;
- private _submitted:boolean = false;
- constructor(private _categoryDataService:CategoryprojectDataService,
- private _router:Router,
- private _activatedRoute:ActivatedRoute) {}
- private _resetCategory(){
- this._category = new categoryproject();
- this._category.project_category_name = '';
- }
- public ngOnInit() {
- this._resetCategory();
- this._parameters = this._activatedRoute.params.subscribe(params => {
- if(typeof params['id'] !== "undefined") {
- this._id = Number.parseInt(params['id']);
- this._categoryDataService.getCategoryById(this._id)
- .subscribe(
- response => {
- this._category = response;
- this._mode = 'update';
- },
- error => {
- this._errorMessage = error.data;
- }
- );
- } else {
- this._mode = 'create';
- }
- });
- }
- public ngOnDestroy() {
- this._parameters.unsubscribe();
- this._category = new categoryproject();
- }
- public onSubmit(form: NgForm) {
- this._submitted = true;
- if(this._mode == 'create') {
- console.log("ini data form "+form);
- this._categoryDataService.addCategory(form.value)
- .subscribe(
- result => {
- if(result.status) {
- console.log(result.status);
- this._router.navigate(['/categoryproject']);
- } else {
- console.log(result.status);
- this._submitted = false;
- }
- },
- error => {
- this._submitted = false;
- this._errorMessage = error.data;
- }
- );
- } else if(this._mode == 'update') {
- this._categoryDataService.updateCategoryById(this._category)
- .subscribe(
- result => {
- if(result.status) {
- this._router.navigate(['/categoryproject']);
- } else {
- this._submitted = false;
- }
- },
- error => {
- this._submitted = false;
- this._errorMessage = error.data;
- }
- );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement