0

I'm new in Angular 2.

I have these radio buttons:

<input type="radio" name="jobb" value="Bonds">
<input type="radio" name="jobb" value="Income">

I want to call a function on myComponent Class with something like this

<input type="radio" name="jobb" value="Bonds" (change)=doSomething()>

where

doSomething(){
 console.log(`Hello`);
}

The functions is not called. I have tried with (click) too. But it doesn't work

asked Mar 8, 2017 at 11:33
1
  • 3
    check error in console. Do you see any? Commented Mar 8, 2017 at 11:41

3 Answers 3

2

You are probably missing the double quotes.

(change)=doSomething() should be (change)="doSomething()"

Have a look at the following plunkr in src/test-item-radio.ts:

@Component({
selector: 'test-item-radio',
template: `
 <div>
 Answer: {{ answer }}
 </div>
 <div class="radio" *ng-for="#option of itemData">
 <label>{{option.id}}
 <input type="radio" [value]="option.id"
 (change)="doSomething()" name="radio-list" [checked]='answer==option.id'>
 <span>{{ option.name }}</span>
 </label>
 </div>
`,
directives: [FORM_DIRECTIVES, NgFor],
providers: []
})
export class TestItemRadio {
@Input()
itemData;
answer: string;
constructor() {
 console.log(this.itemData);
 this.answer=2;
}
doSomething() {
 alert('hello');
}
}

http://plnkr.co/edit/F4ukpS2lExNJvb1CYwbF?p=info

answered Mar 8, 2017 at 11:53
Sign up to request clarification or add additional context in comments.

2 Comments

directives on @Component are deprecated now in latest Angular version
@ranakrunal9 Seems to be alpha verison, so there are other "issues" as well: *ng-for="#option of itemData"
1

change the event from (change) to (click) and include the function in double quotes "dosomething()" like below code

pass a value of radio button to differentiate from one radiobutton with another in the function like "dosomething('Bonds')"

like below code

<input type="radio" name="jobb" value="Bonds" (click)="doSomething('Bonds')">
<input type="radio" name="jobb" value="Income" (click)="doSomething('Income')">

write the function in the component class

doSomething(value:string):void {
 alert(value);
}

I think this should work

answered Mar 25, 2017 at 15:25

Comments

0

You can get radio value by passing $event.target.value as argument for doSomething function as below :

<input type="radio" name="jobb" value="Bonds" (change)="doSomething($event.target.value)">Bonds
<input type="radio" name="jobb" value="Income" (change)="doSomething($event.target.value)">Income

function in Component :

doSomething(data: any){
 console.log('Hello ' + data);
}

You can check Plunker at : https://plnkr.co/edit/MqUD0fGCUcF7brMEx1rq

answered Mar 8, 2017 at 11:44

1 Comment

OP says The functions is not called :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.