1

Facing some Issue with Radio button ,checkboxes And Select/option Binding in angular2.

  1. In case of Radio button why the radio button having false value is not even 'selected' although the value in the model gets updated with false as shown in plukr. but radio button ramains unSelected why so ?
<input type="radio" name="status" (click)="is_active = false"> //not working    
<input type="radio" name="status" (click)="is_active = true"> //working

but

<input type="radio" name="status" (click)="is_active = 'false'"> // working    
<input type="radio" name="status" (click)="is_active = true"> //working

1.1 Secondly how to remove controls (validation) of radio buttons after form submit (radio button getes selected even after form submit, i am using ngModel for form submitting).

  1. In case of checkboxes i am bit confused how to get the values of checked checkbox. i dont want to use (click) method to get checked values.
<input type='checkbox' value='1' [checked]='is_checked = one'>One
<input type='checkbox' value='2' [checked]='is_checked = two'>Two
<input type='checkbox' value='3' [checked]='is_checked = three'>Three

i want when the checkbox gets checked value of checked checkbox push into array of checked checkbox otherwise it will splice (above code is not working i know).

<input type='checkbox' value='1' [checked]='checked("one")'>One
<input type='checkbox' value='2' [checked]='checked("two")'>Two
<input type='checkbox' value='3' [checked]='checked("three")'>Three

till now i am using this method and in class i am checking value of checkbox using javascript .checked, if true pushing into array otherwise splicing from array via Index.

Plunk of my working area for radio and checkbox http://plnkr.co/edit/OXnj6U3udgkKYRAVCmkp

  1. In case of Select/option everything works fine for me. but i want something like this:

    Route1: Having Few Select/option on Route1. User select few option among of them.

    Then User naviagte To another Route using Routing.

    But i want when User come back to Route1 page gets refreshed or either all the select/option on Route1 get unSelected as on first time.

As @GünterZöchbauer said in Answer i had try the same but again not successful. i am trying this code.

export class LsRules implements OnInit, CanReuse {
   .....Some Code Here...
routerCanReuse(next: ComponentInstruction, prev: ComponentInstruction) {
        console.log('routerCanReuse called'); 
        return false; 
    }
   .....Some Code Here...
}

but not even single this console is worked for me. also what is ComponentInstruction here i don't know. where i am wrong ?

Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
  • I'm afraid your plunker is not working. – micronyks Mar 01 '16 at 09:44
  • i have update the plunkr check it. – Pardeep Jain Mar 01 '16 at 09:46
  • You done with checkbox? or should I guide you? – micronyks Mar 01 '16 at 10:15
  • and for 3 question what problem you face? – micronyks Mar 01 '16 at 10:19
  • no if you can please guide me about checkbox. and for 3rd question i want to reset/refersh whole page after routing when user click back button of browser. – Pardeep Jain Mar 01 '16 at 10:25
  • I have posted my answer for checkbox question no 2. I understand but tell me how can we help you without having code for what actually happens and what you actually want. I hope you understand that it is not easy to answer for 3rd question. – micronyks Mar 01 '16 at 11:22
  • The `ComponentInstruction` is basically the info from the route where the navigation was initiated (`prev`) and which route is going to be activated (`next`). If you unconditionally want the component to be reinitialized then just return `false`. – Günter Zöchbauer Mar 02 '16 at 06:46
  • yes i did the same but not working for me. even `ComponentInstruction` is not showing in console why ?. – Pardeep Jain Mar 02 '16 at 06:51

3 Answers3

2

1)

Radio buttons have a few issues.

I got it working like

<input type="radio" [ngModel]="{checked: model.sex == 'male'}" (ngModelChange)="model.sex='male'"  name="sex" value="male">Male<br>
<input type="radio" [ngModel]="{checked: model.sex == 'female'}"  (ngModelChange)="model.sex='female'" name="sex" value="female">Female

for a similar question How to bind to radio buttons in angular2 beta 6

2) You can use the (change) event instead to get the value

3) I guess you you get what you want when you implement

routerCanReuse(next: ComponentInstruction, prev: ComponentInstruction) { return false; }

See also https://angular.io/docs/ts/latest/api/router/CanReuse-interface.html

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
1

I think SO was down for few minutes. For checkbox problem you can do it.

<input type='checkbox' value='1' [(ngModel)]='is_checked_one' (change)="myFun('one')" >One
<input type='checkbox' value='2' [(ngModel)]='is_checked_two' (change)="myFun('two')" >two


export class AppComponent {
   is_active:boolean;
   is_checked_one:boolean=false;
   is_checked_two:boolean=false;

  val:Array<any>= [];
    constructor() { 
      console.log('Component called');
    }

    myFun(selected)
    {
      console.log(this.is_checked_one + " " + selected);
        if('one'===selected)
        {
          if(this.is_checked_one===false)
          {
            console.log('one if');
            this.val.push('one');
          }
          else
          {
            console.log('one else');
            let index=  this.val.indexOf('one');
            console.log(index);
            this.val.splice(index,1);
          }
        }
        else
        {
          if(this.is_checked_two==false)
          {
            console.log('two if');
            this.val.push('two');
          }
          else
          {
            console.log('two else');
            let index=  this.val.indexOf('two');
            console.log(index);
            this.val.splice(index,1);
          }
        }
        }
        }

    }
}

working Demo

micronyks
  • 54,797
  • 15
  • 112
  • 146
  • I'm only adding values to the array. I hope else you can figure out on your own. – micronyks Mar 01 '16 at 11:20
  • whats the involvement of `[(ngModel)]` here ? secondly what if user uncheck the checkbox and checked again ? this is not proper answer i think. is't it ? – Pardeep Jain Mar 01 '16 at 11:36
  • ngModel creats new scope or new model for each checkbox and allows you to work with them separately as one checkbox can be checked and unchecked. So you cant go ahead with only one model or without model. Ok give me some time i ll update my answer soon for deselecting checkbox.... – micronyks Mar 01 '16 at 11:45
  • your code works fine without `ngModel` too. and where have you decalre model for checkbox in example ? – Pardeep Jain Mar 01 '16 at 12:10
  • Now check and understand why did I use `[(ngModel)]`.. and update me if anything is not upto the mark.... – micronyks Mar 01 '16 at 12:21
  • sorry but this one is too long just for checkbox. and read my question again i already using this method of javascript by checking equal or not and then pushing into array according to same. and if we talk about your answer what if we have loop and have to use *ngFor ? we dont know about how many check boxes are to be displayed. so in that case this one is fail.because we have to define ngModel for each. – Pardeep Jain Mar 01 '16 at 12:29
  • Ok tell me how would you figure out whether checkbox is checked or not? is there any way in angular without using `ngModel`!!! now what you want is if checkbox is checked, insert item to array else remove it if already exists. Question seems pretty good but may be can't understand what are you trying to accomplish or may be I'm unable to help you. But good luck and update me here if you get something relevant. – micronyks Mar 01 '16 at 12:35
  • yeah sure here is example owithout `ngModel` http://plnkr.co/edit/1IJnarBwez8ALQMv0Shl?p=preview . but my question is, is there any way apart from this checking for `.checked` true/false. with or without ngModel ? – Pardeep Jain Mar 01 '16 at 16:54
  • I have checked your link. But you have done it using javascript way and my question was using angular.. So, I think when you work with radio, checkbox I think ngModel is required which can provide current status of control. And for you question, YES it is possible using `ngModel`. `ngModel` will give you current status of checkbox like true or false without using `.checked`. Don't you think? – micronyks Mar 02 '16 at 04:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105075/discussion-between-pardeep-jain-and-micronyks). – Pardeep Jain Mar 02 '16 at 05:04
1

PrimeNG radio buttons simplifies this by allowing binding to a model value. http://www.primefaces.org/primeng/#/radiobutton

Cagatay Civici
  • 6,406
  • 1
  • 29
  • 34