set checkbox to true on angular

0

I have several checkbox but I can not make the value% co_from% false in my model change to booleano .

in my component.ts I have the model initialized in this way

this.listaMembresia = new ItemMembresia(0, "", 0, 0, 0, 0, false);

in the html I have done it in several ways and the value does not change

<input type="checkbox" name="limite_semana" [checked]="listaMembresia.limite_semana" >
<input type="checkbox" name="2" [(ngModel)]="listaMembresia.limite_semana">
<input type="checkbox" name="limite_semana" [value]="true" [checked]="listaMembresia.limite_semana" >

I've tried it in these ways and I can not get results, I hope someone can help me

export class ItemMembresia 
{
    constructor(
        public id:Number,
        public nombre:String,
        public costo:Number,
        public duracion:Number,
        public tiempo:Number,
        public limite_por_dia:Number,
        public limite_semana:Boolean
    ){}
}
    
asked by Christian Eduardo Amaya Rivas 17.06.2018 в 03:05
source

3 answers

0

Your problem is very similar to one that I solved recently. I tried to assign values from code to checkboxes and selection lists and my scripts failed (looking correct in light of the documentation I knew). My case differs from yours in that I tried to assign values using JQuery via $(selector).attr(nombre_atributo, valor) reading the JQuery documentation I found a comment about it (I do not remember the link) which indicated that it should be done via $(selector).prop(nombre_propiedad, valor) where name_property = attribute_name. I used it and problem solved, my assignments via code effectively change the value attribute of the elements when I need it.

They offered as a justification to use prop instead of attr a comment similar to the following: the attributes of the html elements are preserved, but their properties do not change. If the attributes could be modified, it would be impossible to reset a page to its original state. So we must act on properties rather than on attributes, although there is a background pseudo-coincidence between attributes and properties; something like: the attribute is the value of the property at the moment of constructing the object, while the property is its present value.

And, for language abuse, we refer to that duality of value by using the term attribute generically, without realizing that this is not its present value. A boring subtlety that makes us waste hours and hours looking for errors that do not exist.

I hope this suggestion is useful.

    
answered by 17.06.2018 в 04:43
0

You could share more information about the constructor and the attributes of the ItemMembresia class. I just did a test with [(ngModel)] and I have no problem.

<input type="checkbox" [(ngModel)]="test.limite_semana">

And in the ts:

public test: any = { limite_semana: false };
    
answered by 17.06.2018 в 04:53
0

Try this:

export class ItemMembresia 
{
    public id:Number;
    public nombre: String;
    public costo: Number;
    public duracion: Number;
    public tiempo: Number;
    public limite_por_dia: Number;
    public limite_semana: Boolean;

    constructor(){}
}
    
answered by 17.06.2018 в 07:54