Skip to content

Commit

Permalink
docs: fix code style (#45527)
Browse files Browse the repository at this point in the history
I have removed unnecessary code from the components & i initialize the
form in OnInit function (life cycle). I think initilization of forms and
getting data from service must be done inside ngOnInit.

PR Close #45527
  • Loading branch information
Piyush132000 authored and jessicajaniuk committed Jun 14, 2022
1 parent ce74bea commit d2470cc
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
Expand Up @@ -8,7 +8,7 @@ import { products } from '../products';
})
export class ProductListComponent {

products = products;
products = [...products];

share() {
window.alert('The product has been shared!');
Expand Down
Expand Up @@ -10,7 +10,7 @@ import { products } from '../products';
// #docregion on-notify
export class ProductListComponent {

products = products;
products = [...products];

share() {
window.alert('The product has been shared!');
Expand Down
Expand Up @@ -7,6 +7,4 @@ import { Component } from '@angular/core';
})
export class ShippingComponent {

constructor() { }

}
@@ -1,7 +1,8 @@
// #docplaster
// #docregion imports
import { Component } from '@angular/core';
import { Component, OnInit } from '@angular/core';

import { Observable } from 'rxjs';
import { CartService } from '../cart.service';
// #enddocregion

Expand All @@ -11,15 +12,18 @@ import { CartService } from '../cart.service';
styleUrls: ['./shipping.component.css']
})
// #docregion props
export class ShippingComponent {
export class ShippingComponent implements OnInit {

shippingCosts = this.cartService.getShippingPrices();
shippingCosts!: Observable<{ type: string, price: number }[]>;
// #enddocregion props

// #docregion inject-cart-service
constructor(private cartService: CartService) { }
// #enddocregion inject-cart-service
// #docregion props

ngOnInit(): void {
this.shippingCosts = this.cartService.getShippingPrices();
}

// #docregion props
}
3 changes: 2 additions & 1 deletion aio/content/start/start-data.md
Expand Up @@ -266,7 +266,8 @@ This section guides you through modifying the `ShippingComponent` to retrieve sh

<code-example header="src/app/shipping/shipping.component.ts" path="getting-started/src/app/shipping/shipping.component.ts" region="inject-cart-service"></code-example>

1. Define a `shippingCosts` property that sets the `shippingCosts` property using the `getShippingPrices()` method from the `CartService`.
1. Define a `shippingCosts` property that sets the `shippingCosts` property using the `getShippingPrices()` method from the `CartService`.
Initialize the `shippingCosts` property inside `ngOnInit()` method.

<code-example header="src/app/shipping/shipping.component.ts" path="getting-started/src/app/shipping/shipping.component.ts" region="props"></code-example>

Expand Down

0 comments on commit d2470cc

Please sign in to comment.