In Angular 5, for input
<input name="techSpecMeta.make" [(ngModel)]="techSpecMeta.make" type="text" class="form-control border-input" placeholder="Enter car brand">
getting error Cannot read property 'make' of undefined at Object.eval [as updateDirectives]
export class UserComponent implements OnInit {
constructor(private vahinfo: VehicleInfo) {}
ngOnInit() {}
techSpecMeta: {};
onSave = function(vehicle, isValid: boolean) {
this.vahinfo.saveVehicle(vehicle).subscribe(data => {
console.log(data.data)
}, error => this.errorMessage = error)
}
}
To solve the "Cannot read properties of undefined" error, make sure that the DOM element you are accessing exists. The error is often thrown when trying to access a property at a non-existent index after using the getElementsByClassName() method.
What Causes TypeError: Cannot Read Property of Undefined. Undefined means that a variable has been declared but has not been assigned a value. In JavaScript, properties and functions can only belong to objects.
The "Cannot read property '0' of undefined" error occurs when trying to access the 0th index in a variable that stores an undefined value. Make sure to initialize the variable to the correct type, e.g. array or string, before accessing the index.
To solve the "Cannot read property 'value' of null" error, make sure you aren't accessing the value property on a null value, e.g. a non-existent DOM element. An element with the provided id does not exist in the DOM, so the getElementById method returns null .
techSpecMeta: {};
In Type script this means to declare a property of type {}
with no value initialized. It is the same as:
techSpecMeta: Object;
You should instead be doing
techSpecMeta = {};
To make the binding work, you will need the property make
as well.
techSpecMeta = {make: null};
Ideally you would create a class/interface for TechSpecMeta
class TechSpecMeta {
make: null;
anotherProperty: null;
}
and use it in your component
techSpecMeta = new TechSpecMeta();
please try to intialize it as below description
public techSpecMeta = <any>
{};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With