Angular Project Notes
Angular Project Notes
code:
//Bugdet.component.ts
onNumberOfYearsChange(selectedItems:any) {
debugger
this.selectedNumberOfYears = selectedItems[0].item_id;
var previousYear=this.getPreviousYears();
return { title: item, value: item }; // Assuming 'title' and 'value' are the same as in selectedItems
});
let ODataFilters = {
"Company": this.myForm.get('idCompany')!.value,
"BuisnessUnit":businessUnits,
"PreviousYearsArr":previousYear
};
sessionStorage.setItem("ODataFilters", JSON.stringify(ODataFilters));
this.myForm.get('percentage')?.reset();
this.dataFetching();
this.call_OData_ACC_BAL();
this.updateSelectYearDropdownData();
call_OData_ACC_BAL(){
let q:any;
if (storedData) {
try {
q = JSON.parse(storedData);
if (typeof q !== 'object' || Array.isArray(q)) {
catch (error) {
q = {};
this.loading = true;
if (q.Company.length > 0) {
var Count = 0;
if (Count == 0) {
else {
Count++;
})
var Count = 0;
q.BuisnessUnit.forEach((x:any)=>{
if(Count == 0)
else
Count++;
})
aFilter += ")";
this.odataService.oData_ACC_BAL(oDataUrl).subscribe((x:any)=>{
this.loading = false;
this.dataSet = []
this.dataSet = x.d.results;
this.monthWisePNLDataset(this.dataSet,selectedYear);
this.monthWiseBalanceSheetDataSet(this.dataSet,selectedYear);
})
//topbar.ts
localStorage.setItem('mapParameter', '/ACPLANACT_CDS');
//odata.service.ts
oData_ACC_BAL(odataUrl: string) {
console.log("mapParameter", mapParameter);
console.log("url", url)
// res.setHeader('Access-Control-Allow-Origin', '*');
ZSOL_MASTER_DATA() {
debugger;
'Authorization': authString,
'Access-Control-Allow-Origin' : '*'
});
// res.setHeader('Access-Control-Allow-Origin', '*');
return result;
- Explanation of oData_ACC_BAL
• var url = document.location.origin + mapParameter + odataUrl;: This line constructs the full URL
for the OData request by concatenating the origin of the current document, the value of
mapParameter, and the odataUrl parameter.
• console.log("url", url);: This line logs the constructed URL to the console for debugging purposes.
• var result = this.http.get<any>(url);: This line makes an HTTP GET request to the constructed URL
using Angular's HttpClient service. The get method returns an observable that emits the HTTP
response.
• return result;: This line returns the observable result from the method oData_ACC_BAL.
-Explanation of ZSOL_MASTER_DATA :
• var location = document.location.origin;: This line retrieves the origin (protocol, hostname, and
port number) of the current URL and stores it in the variable location. It is similar to what we
discussed earlier with document.location.origin.
• const username = 'Solsynch'; and const password = 'Sol23@S4prddb1';: These lines define the
username and password for basic authentication to access the OData service. It seems like the
service requires basic authentication.
• const authString = 'Basic ' + btoa(username + ':' + password);: This line constructs the
Authorization header value for basic authentication. It encodes the username and password in
Base64 format as per the basic authentication standard.
• var result = this.http.get<any>(url, { headers });: This line makes an HTTP GET request to the
constructed URL (url) using Angular's HttpClient service (this.http). It includes the custom
headers (headers) for authentication and CORS. The <any> type parameter indicates that the
response body is expected to be of any type.
• return result;: This line returns the result of the HTTP GET request as an observable. The caller of
this method can subscribe to this observable to receive the response from the server
asynchronously.
• In summary, this function constructs a URL for an OData service endpoint, sets up basic
authentication headers, makes an HTTP GET request to the endpoint using Angular's HttpClient,
and returns the result as an observable.
• Disable Caching: Ensure that the browser doesn't cache pages that require authentication. You
can do this by setting appropriate HTTP headers to disable caching for sensitive pages.
• Redirect After Logout: After successfully logging out, redirect the user to a page that doesn't
cache content. For example, you could redirect them to the login page or a neutral landing page
that doesn't have sensitive information.
• Use Route Guards: In Angular, you can implement route guards to prevent unauthorized access
to certain routes. You can create an AuthGuard that checks if the user is logged in before
allowing access to certain routes. If the user is not logged in, they'll be redirected to the login
page.
• Clear Session Data: Upon logout, make sure to clear any session data or tokens that indicate the
user is logged in. This ensures that even if the user navigates back using the browser's history,
they won't be considered authenticated.
Code:-
-// auth.guard.ts
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
canActivate(): boolean {
if (this.authenticationService.isLoggedIn()) {
return true;
} else {
this.router.navigate(['/userLogin']);
return false;
-// authentication.service.ts
return localStorage.getItem('currentUser');
}
isLoggedIn(): boolean {
return !!localStorage.getItem('currentUser');
logout() {
-//userlogout.component.ts
@Component({
selector: 'app-user-logout',
templateUrl: './user-logout.component.html',
styleUrl: './user-logout.component.css'
})
this.authenticationService.logout();
this.router.navigate(['/userLogin']);