Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
19
rated 0 times [  23] [ 4]  / answers: 1 / hits: 16832  / 5 Years ago, fri, january 25, 2019, 12:00:00

I am Using Angular 7. i run this cmd ng build --prod build for protection.



That time i am caching this Error( Property 'service' is private and only accessible within class 'LoginComponent'):





ERROR in srcapploginlogin.component.html(5,33): : Property 'service' is private and only accessible within class 'LoginComponent'.
srcapploginlogin.component.html(18,104): : Property 'service' is private and only accessible within class 'LoginComponent'.





It's my HTML Code:





<div id=login_section class=d-flex justify-content-center align-items-center>
<div class=login_cnt col-8 row>
<div class=col-6 d-flex justify-content-center py-4>

<form class=col-8 [formGroup]=service.loginform>
<h2 class=text-center>User Login</h2>
<mat-form-field class=col-12>
<input matInput type=text placeholder=Username formControlName=username >
<mat-error>Username is Required</mat-error>
</mat-form-field>

<mat-form-field class=col-12>
<input matInput [type]=hide ? 'password' : 'text' formControlName=password placeholder=Password>
<mat-icon matSuffix (click)=hide = !hide>{{hide ? 'visibility_off' : 'visibility'}}</mat-icon>
<mat-error>Password is Required</mat-error>
</mat-form-field>
<a href=# class=float-left lnk_forgot h7>Forgot Password</a>
<button mat-raised-button color=primary class=float-right [routerLink]=['/home'] [disabled]=service.loginform.invalid>Login</button>
</form>
</div>
</div>
</div>





It's My TS File:





import { Component, OnInit } from '@angular/core';
import { LoginService } from '../shared/login.service';

@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
username : string;
password: string;
hide = true;

constructor(private service: LoginService) { }
ngOnInit() {
}

}





when running ng serve am not catching it .


More From » html

 Answers
75

There are 2 ways to resolve this.

1.

Change private service: LoginService to public service: LoginService

As you are using AOT during compilation, you can not access private properties of your component in your HTML template.



2.



If you want to keep your service private, you can provide a public method in the controller which returns the service properties. You can call this method from your HTML template. It would be something like this:



Template:



<div id=login_section class=d-flex justify-content-center align-items-center>
<div class=login_cnt col-8 row>
<div class=col-6 d-flex justify-content-center py-4>

<form class=col-8 [formGroup]=getLoginForm()> <!-- Change here-->
<h2 class=text-center>User Login</h2>
<mat-form-field class=col-12>
<input matInput type=text placeholder=Username formControlName=username >
<mat-error>Username is Required</mat-error>
</mat-form-field>

<mat-form-field class=col-12>
<input matInput [type]=hide ? 'password' : 'text' formControlName=password placeholder=Password>
<mat-icon matSuffix (click)=hide = !hide>{{hide ? 'visibility_off' : 'visibility'}}</mat-icon>
<mat-error>Password is Required</mat-error>
</mat-form-field>
<a href=# class=float-left lnk_forgot h7>Forgot Password</a>
<button mat-raised-button color=primary class=float-right [routerLink]=['/home'] [disabled]=getLoginForm().invalid>Login</button> <!-- Change here-->
</form>
</div>
</div>
</div>


Controller:



import { Component, OnInit } from '@angular/core';
import { LoginService } from '../shared/login.service';

@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
username : string;
password: string;
hide = true;

constructor(private service: LoginService) { }
ngOnInit() {
}

getLoginForm() {
return this.service.loginform;
}
}


P.S: I have not tested the second one myself for the moment.


[#52714] Monday, January 21, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sageallenv

Total Points: 458
Total Questions: 102
Total Answers: 104

Location: Venezuela
Member since Thu, Jul 15, 2021
3 Years ago
sageallenv questions
Wed, Jun 16, 21, 00:00, 3 Years ago
Fri, Apr 10, 20, 00:00, 4 Years ago
Sun, Feb 2, 20, 00:00, 4 Years ago
;