I'm trying to bind viewmodels to a view through component. Initially I did this in the component and it was almost working:
model: any = {
TenantId: '',
CustomFieldName: '',
quantities: []
};
quantity: any = {
price: '',
name: ''
}
Then on button click for adding new object of
model
I was doing this:
addNewQuantity() {
if (this.newQuantity) {
this.quantity.name = this.newQuantity;
this.model.quantity.push(this.quantity);
this.newQuantity = '';
}
}
The issue with above event was that the same quantity object was added for every new click.
What I did next is that I created two models:
Pricegridquantity viewmodel:
export class PriceGridQuantity {
price: string;
name: string;
}
Pricegrid viewmodel
import { PriceGridQuantity } from './pricegridquantity';
export class PriceGridModel {
TenantId: number;
CustomFieldName:string;
PriceList: Array <PriceGridQuantity> ; }
Now I'm trying to link everything in the component, I have this:
model: PriceGridModel[] = [];
Is the statement above correct to initialize a new VM of type PriceGridModel?
If no, please let me know what is better.
And 2nd, on the button click to add new object of type PriceGridQuantity to PriceGridModel, what is the correct way of initializing and then adding object of type PriceGridQuantity to the PricegridModel?