admin管理员组文章数量:1414949
I have a two-choice radio group that I want to get a selected value of and pass it as one of the parameters to navigate to. In my earlier semi-related question someone suggested this solution: Angular2 - Radio Button Binding but I've been trying and failing to use it:
searchponent.html:
<form autoplete="on" class="form-inline">
<input style="border-color: white; max-width: 300px" name="name" autofocus type="text" #name placeholder="Name..."
class="form-control">
<div class=" btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-toggle active">
<input type="radio" [(ngModel)]="realm" name="realm" [ng-value]="'Feronis'" autoplete="off" checked> Feronis
</label>
<label class="btn btn-toggle">
<input type="radio" [(ngModel)]="realm" name="realm" [ng-value]="'Angrathar'" autoplete="off"> Angrathar
</label>
</div>
<button (click)="onSearch(name.value,realm)" class="btn btn-search">
<span class="fa fa-search"></span></button>
I also tried with ngValue
and value
.
searchponent.ts:
export class SearchComponent implements OnInit {
realm: string;
onSearch(nameIn: string, realmIn: string) {
console.log(this.realm); // undefined
let nameCase = nameIn.charAt(0).toUpperCase() +
nameIn.slice(1).toLowerCase();
let realmCase = realmIn.charAt(0).toUpperCase() + // realmIn also undefined
realmIn.slice(1).toLowerCase();
this.router.navigate(['/character', realmCase, nameCase])
}}
Can't bind to 'ngValue' since it isn't a known property of 'input'.
I also have imports for
import {FormsModule} from "@angular/forms";
in app.module.ts
and searchponent.ts
I have a two-choice radio group that I want to get a selected value of and pass it as one of the parameters to navigate to. In my earlier semi-related question someone suggested this solution: Angular2 - Radio Button Binding but I've been trying and failing to use it:
search.ponent.html:
<form autoplete="on" class="form-inline">
<input style="border-color: white; max-width: 300px" name="name" autofocus type="text" #name placeholder="Name..."
class="form-control">
<div class=" btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-toggle active">
<input type="radio" [(ngModel)]="realm" name="realm" [ng-value]="'Feronis'" autoplete="off" checked> Feronis
</label>
<label class="btn btn-toggle">
<input type="radio" [(ngModel)]="realm" name="realm" [ng-value]="'Angrathar'" autoplete="off"> Angrathar
</label>
</div>
<button (click)="onSearch(name.value,realm)" class="btn btn-search">
<span class="fa fa-search"></span></button>
I also tried with ngValue
and value
.
search.ponent.ts:
export class SearchComponent implements OnInit {
realm: string;
onSearch(nameIn: string, realmIn: string) {
console.log(this.realm); // undefined
let nameCase = nameIn.charAt(0).toUpperCase() +
nameIn.slice(1).toLowerCase();
let realmCase = realmIn.charAt(0).toUpperCase() + // realmIn also undefined
realmIn.slice(1).toLowerCase();
this.router.navigate(['/character', realmCase, nameCase])
}}
Can't bind to 'ngValue' since it isn't a known property of 'input'.
I also have imports for
import {FormsModule} from "@angular/forms";
in app.module.ts
and search.ponent.ts
- ng-value is not a valid property on input element, you have to instead use value property in either [value]="expressnion" or value="actual-value" way. – Varun Singh Commented Feb 1, 2018 at 9:58
-
@varunsingh I'll copy my other ment: I get
ERROR TypeError: Cannot read property 'charAt' of undefined
when I doonSearch(name.value,realm)
withvalue="Feronis"
. The console.log is also undefined – Asalas77 Commented Feb 1, 2018 at 10:01
2 Answers
Reset to default 2Here is the working example: https://plnkr.co/edit/9YOV50bV1E9F9tH3Uw8a
It seems everything is fine except the fact that you want first radio button to be pre-selected.
You are doing pre-selection using checked property. Now since you are using [ngModel]="realm"
, ngModel sets initial value. In your case initial value of realm is undefined, none of the radion button gets pre-selected.
Set some value in realm in your class, to get pre-selected radio button as shown in plunker above.
Simply use value
instead of [ng-value]
for example
value="Feronis"
Because as you specify [ng-value]="'Feronis'"
here you trying attribute binding with static string
which is equal to simply assign of value using value attribute like this
value="Feronis"
Solution for : ERROR TypeError: Cannot read property 'charAt' of undefined
No need to send value of radio selected as param just fetch the value using this.realm
like this
console.log(nameIn, this.realm);
and your search
function looks like
(click)="onSearch(name.value)"
Working code
<form autoplete="on" class="form-inline">
<input style="border-color: white; max-width: 300px" name="name" autofocus type="text" #name placeholder="Name..." class="form-control">
<div class=" btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-toggle active">
<input type="radio" [(ngModel)]="realm" name="realm" value="Feronis" autoplete="off" checked> Feronis
</label>
<label class="btn btn-toggle">
<input type="radio" [(ngModel)]="realm" name="realm" value="Angrathar" autoplete="off"> Angrathar
</label>
</div>
<button (click)="onSearch(name.value)" class="btn btn-search">
<span class="fa fa-search"></span></button>
</form>
onSearch(nameIn: string) {
console.log(nameIn, this.realm);
}
本文标签:
版权声明:本文标题:javascript - Angular: How to get data from radio button group and pass it as a parameter to a method? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745149944a2644873.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论