Visibility of Input-properties in Angular 2

I am encountering an issue where a component parent is calling another component child with an input-property.

Although the property is available in the child's template, it does not seem to be accessible within the constructor or OnInit functions. Is this normal behavior, or have I made an error in my implementation?

parent.component.ts

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

@Component({
  selector: "parent",
  template: `<child [name]="'foobar'"></child>`
})

export class ParentComponent
{
}

child.component.ts

import {Component, Input, OnInit} from '@angular/core';

@Component({
  selector: "child",
  template: "name={{ name }}"
})

export class ChildComponent
{
  @Input () name:string="init";

  constructor ()
  {
    console.log ("constr: " + name);
  }

  ngOnInit ()
  {
    console.log ("oninit: " + name);
  }
}

EDIT

  • Updated my sample code and placed @Input inside the class.

  • Revised my example to real code. While the template displays "foobar", the console output shows an empty string.

Answer â„–1

Modify

console.log ("oninit: " + name);

into

console.log ("oninit: " + this.name);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Circular reference in Angular/TypeScript

I encountered a circular dependency issue in my Angular project and tried various solutions, including exporting all dependent classes from a "single file" as suggested here. Unfortunately, this approach did not work for me. I then explored other solutions ...

Operating on Javascript Objects with Randomized Keys

Once I retrieve my data from firebase, the result is an object containing multiple child objects. myObj = { "J251525" : { "email" : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6c3823212 ...

Tips for creating a custom hook that is type safe:

When I use the custom function createUser, I've noticed that I can pass numbers instead of strings without receiving an error. Surprisingly, even if I forget to include an argument, no red squiggles appear. const [userState, createUser] = useCre ...

Is it possible to integrate @google-cloud/logging into an Ionic4/Angular8/Firebase client application? Tips for resolving module import issues

I am trying to implement nodejs-logging in my app using Ionic 4, Angular 8, and Firebase for writing logs to StackDriver. In the root of my app, I have taken the following steps: Installed @google-cloud/logging using npm Navigated to @google-cloud/loggi ...

Encountering Angular 8 error TS2304 at line 39: Cannot find the specified name after shutting down WebStorm

After working on my Angular project and closing the IDE last night, I encountered an odd problem today. The project doesn't seem to recognize certain libraries such as Int8Array, Int16Array, Int32Array... And many others. While the project is still ab ...

Can you guide me on implementing AWS SDK interfaces in TypeScript?

Attempting to create an SES TypeScript client using AWS definitions file downloaded from this link My approach so far: /// <reference path="../typings/aws-sdk.d.ts" /> var AWS = require('aws-sdk'); var ses:SES = new AWS.SES(); The error ...

Accessing information from an Odata controller in Angular2

Greetings as a first-time question asker and long-time reader/lurker. I've been delving into learning angular2, but I'm facing some challenges when it comes to retrieving data from an odata controller. In my simple Angular 2 application, I'm ...

Creating Dynamic Templates in Angular 6

We are currently rendering the template dynamically in our application by utilizing AngularJS's $compile to compile HTML on the fly. I am wondering if there is a similar feature in Angular 6 that allows us to manually compile HTML. I have searched for ...

Passing an array of objects as properties in React components

My functional component looks like this: function ItemList({ items }: ItemProps[]) { return <p>items[0].name</p> } Here is how I'm creating it: <ItemList items={items} /> The array items contains objects in the format [{name: &ap ...

How to effectively handle null values using try..catch statement in typescript

As a beginner, I am learning how to write a try/catch statement in TypeScript. My issue is that there is a function within the "try" block that returns null. How can I implement code in the "catch" block specifically for when the function in "try" returns ...

You cannot invoke this expression while destructuring an array of React hooks in TypeScript

Within my React TypeScript component, I have several fields that check a specific condition. If the condition is not met, the corresponding field error is set to true in order to be reflected in the component's DOM and prevent submission. However, whe ...

Submitting JSONL String to a REST API using Angular: A Guide for Sending Data as a File via POST Method

I am currently developing an Angular App that integrates with OPENAI APIs, specifically focusing on their Upload function. The API Endpoint for this function is named "files" and you can find detailed documentation here When testing the request using Pos ...

Creating an Angular Universal Dockerfile and docker-compose.yml file: A step-by-step guide

After struggling to Dockerize my Angular universal app and integrate it with an existing dockerized Spring Boot REST backend, I found myself hitting a wall in terms of available resources and assistance online. Despite making various adjustments, the Docke ...

Proper method of managing undeclared declaration files (index.d.ts)

I encountered the following error message: error TS7016: Could not find a declaration file for module 'react-native-camera'. '/Users/ilja/Documents/Repositories/blok/node_modules/react-native-camera/index.js' implicitly has an 'an ...

Can you provide instructions on how to display data in two lines within a mat-select field?

Is it possible to show selected options in mat-select with long strings in two lines within the same dropdown? Currently, the string appears incomplete. You can see an example of this issue here: incomplete string example <mat-form-field class="f ...

Discovering the method to access a local function within a static function in Javascript ES6 (ES2015) or Typescript

Is there a way to access the non-static "foo2" method from inside the static "bar" method? So far, I'm only able to access the "foo1" and "foo3" methods. Can anyone provide guidance on how to achieve this? let foo1 = () => { alert('foo1†...

The route configuration is invalid: it is not possible to use 'redirectTo' and 'canActivate' together

I am looking to implement Laravel Angular JWT authentication, and I am encountering an issue with applying a guard. The error message states: Invalid configuration of route '': redirectTo and canActivate cannot be used together. Redirects happen ...

Accessing images hosted on an IIS server from a client using a URL in an ASP .Net application

My application is built using Api ASP.Net, and I store the images in the "wwwroot" directory Content https://i.sstatic.net/JP2Lx.png After publishing my application, the folder structure remains intact and I need to be able to access the images through ...

Resolving search box setup problem in PrimeNG dataView

I am working on integrating p-dataView with Angular 5 but encountering an error Cannot read property 'split' of undefined at DataView.filter Despite checking the documentation, I have been unable to find a solution to this issue. It seems lik ...

the most effective method for including a new field in a formGroup

How can I correctly add a new object property to an Angular formGroup? Currently, my setup looks like this: ngOnInit() { this.form = this.fb.group({ // store is the formGroupname store: this.fb.group({ // branch and code are formControlN ...