Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Problem

How to return EmployeeCode on angular employees component.html ?

CSS
Sample data ReferenceTable

Code  tableName   FieldName   LabelText
111   Employees   EmployeeId  EmployeeCode
Result of calling

GetLabelTextForEmployee('Employees','EmployeeId')


it suppose Return EmployeeCode

I work on asp.net core 2.1 web API with angular 6.

I make function on web API name GetReferenceFileData to get text of label from

database and show on view on employees.component.html based on Reference Table .

This is my function :

C#
[HttpGet("{tableName}/{FieldName}")]
        [Produces("text/plain")]
        public string GetReferenceFileData([FromRoute] string tableName, [FromRoute] string FieldName)
        {
          var Result =  (from h in _context.ReferenceFiles
                          where h.TableName == tableName && h.FieldName == FieldName
                          select h.LabelText).FirstOrDefault();
            if(Result==null)
            {
                Result = "Not Exist";
            }

            return (Result);


        }


Function above return only one string value for label as Scalar Value

on employee.component.html
// how to use function GetLabelTextForEmployee here to return EmployeeCode

What I have tried:

1- on API service on angular I create Function Below :
public GetLabelTextForEmployee(tableName:string,FieldName:string): Observable<any> {
        return this.http
            .get('https://localhost:44326/api/reference/' + tableName + '/' + FieldName, {
                responseType: 'text',
            });
    }
Posted
Updated 27-Feb-19 22:59pm

1 solution

In your employees.component.ts file you import OnInit and OnDestoy and implement them.
On your class, you'll need a employeeCode string-property to store the value.
In ngOnInit you need to subscribe to the ApiService's GetLabelText with the needed parameters and in ngOnDestroy you'll need to unsubscribe.

JavaScript
import { OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs';

import { ApiService } from '../../services/api.service';

@component({
  selector: '[app-employees]'
  templateUrl: './employees.component.html',
})
export class EmployeesComponent implements OnInit, OnDestroy {

  public labelSubscription: Subscription;
  public employeeCode: string;

  public constructor(private apiService: ApiService) { }

  public ngOnInit() {
    this.labelSubscription =
      ApiService.GetLabelText('Employee', 'EmployeeId')
                .subscribe((value) => this.employeeCode = value);
  }

  public ngOnDestroy() {
    if (this.labelSubscription) {
      this.labelSubscription.unsubscribe();
      this.labelSubscription = null;
    }
  }
}


I also suggest you take a look at Angular documentation and do a run on the Heroes tutorial.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900