There are a few ways to achieve this. First, the interaction of components in Angular and how they display on the page is a little less straightforward than the example you're trying to show an example with. Please refer to this link to learn more about the component lifecycle in Angular.
As for setting the values of properties on a component, here are a few examples. Please keep in mind that some of this might need tweaking as I did not write it in an IDE/run the code.
Your example already does this. When the component is created, the default value of that field is already set. title = 'My Title'
Decorating your property with @Input()
allows you to pass data from a parent component to a child component. <my-child-component [title]='value-or-variable'></my-child-component>
Decorating a "component" property (in this case the child component in the parent) with the @ViewChild()
decorator will allow the parent component to have access to the child component and work with it's properties. Keep in mind the component indicated must be a child component of the parent component. @ViewChild(MyChildComponent) child: MyChildComponent;
then after the parent component's init cycle is done this.child.title = 'My Title';
Decorating a "component" property (in this case the child component in the parent or in any descendents) with the @ContentChild()
decorator will allow the parent component to have access to the descendents components and work with their descendents and properties. Keep in mind the component indicated can be any descendents component of this parent. @ContentChild(MyDescendentComponent) descendent: MyDescendentComponent;
then after the init cycles are done this.descendent.title = 'My Title';
Provided objects can be injected into the component and set values for the component ideally in the ngOnInit method. The provider can be set at a few different levels including (but not limited to) the component modules. This link goes far more in depth on dependency injection even though it's slightly older.
class MyComponent {
constructor(myService: MyService) {}
}
//module
providers: [
MyTitleToken,
],
//component
class MyComponent {
constructor(public title: MyTitleToken) {}
}
//module
providers: [
{provide: 'MyTitle', useValue: 'Hello Title'},
],
//component
class MyComponent {
constructor(@Inject('MyTitle') title: string) {}
}