1

I'm reading Angular in Action from Manning. Chapter 2 shows me how to write my first component but the example is outdated. I can't figure out how to update it. I'm using Angular CLI version 7.1.3.

Angular CLI generates this:

import { Component, OnInit } from '@angular/core';
@Component({
    selector: 'app-summary',
    templateUrl: './summary.component.html',
    styleUrls: ['./summary.component.scss']
})
export class SummaryComponent implements OnInit {
    constructor() { }
    ngOnInit() {
    }
}

The book assumes that Angular CLI will generate this:

import { Component, Input } from '@angular/core';
@Component({
  selector: 'summary',
  styleUrls: ['./summary.component.css'],
  templateUrl: './summary.component.html'
})
export class SummaryComponent {
  @Input();
}

What is the difference between @Input and OnInit? How does a person take an input example and translate it into the "OnInit" way of doing things?

2
  • Yes, I tried the message board the publisher provided for the book. No one home. Commented Dec 28, 2018 at 17:25
  • I just checked the book. Page 42 first line is: @Input() stock: any; Commented Dec 28, 2018 at 17:37

3 Answers 3

3

@Input and ngOnInit are two different directives and do not conflict or replace each other.

You can quite easily (manually) add @Input to the generated component and continue as per the book. Just ignore the ngOnInit for now until you learn to use it. It won't hurt to just leave it there.

If you want to know what ngOnInit does, look here

Also notice, if your example deals with the styles, then the CLI has generated sccs but the book has css. sccs should technically take all css and should just work.

Sign up to request clarification or add additional context in comments.

Comments

2

@Input() is a decorator that adds metadata to the class that makes the property, which will be written next to it, available for data-binding.

ngOnInit() is a lifecycle hook that Angular calls shortly after creating the component.

Both still exist in the current version of Angular but do not have utility if they are empty (same for the constructor() method) so you can replace all your code by:

import { Component } from '@angular/core';

@Component({
    selector: 'app-summary',
    templateUrl: './summary.component.html',
    styleUrls: ['./summary.component.scss']
})

export class SummaryComponent  {

}

You'll always be able to add them whenever you want.

Comments

1

@Input is a decorator. It allows you to mark a field as an input argument. You can, for example, pass an argument from a parent component to a child component.

OnInit is an interface. It allows you to handle additional initialization tasks under ngOnInit method, which occurs at component creation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.