Answer the question
In order to leave comments, you need to log in
What is the correct way to instantiate a class in AngularTS?
Good afternoon.
Started to understand with Angular and got stuck on the element operation.
I have a model describing a user:
export class User {
id?: string;
name?: string;
linktoimage?: string;
}
import { User } from '../models/User.model';
import { Injectable} from '@angular/core';
import * as signalR from "@aspnet/signalr";
import { Router } from '@angular/router';
@Injectable()
export class UserAuthService {
url = "http://localhost:52015/auth";
user: User = new User();
private connection = new signalR.HubConnectionBuilder().withUrl(this.url, { accessTokenFactory: () => getToken() }).build();
constructor(private router: Router) {
this.connection.start().catch(err => { });
this.connection.on("updateinfo", (state: boolean, ndata: string) => {
if (state == true) {
let jsonData = JSON.parse(ndata);
this.user.name = jsonData.UserName;
this.user.linktoimage = jsonData.ImageLink;
this.router.navigate(['/dashboard']);
}
});
this.connection.on("GetToken", (token: string) => {
this.user.id = token;
sessionStorage.setItem("token", token);
});
}
}
import { Component } from '@angular/core';
import { UserAuthService } from '../../services/auth.service';
@Component({
selector: 'welcome',
template: `<div><button type="button" class="btn btn-block " (click)="StartAuth()"></button></div>`,
styles: [`:host{
top: 0;
left: 0;
width: 100%;
height: 100%;
}`],
providers: [UserAuthService]
})
export class Welcome {
constructor(private _userauth: UserAuthService) { }
public StartAuth(): void {
var authwindow = window.open("/api/Auth/Authorization?code=123&scope=" + this._userauth.user.id);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question