默认角度2不检查单选按钮

时间:2017-03-02 16:27:48

标签: angular

以下是html文件中的代码:

       <div class="form-group">
            <div class="form-text">Some Question about Email and Phone Details?</div>


            <div>
                <input type="radio" value="1" [formControl]="signUpDetailsForm.controls['details']" name="details" (click)="EmailClicked()"
                ng-model="selectedOption"> Email</div>

            <div>

文字                               两个

在component.ts类中,我将selectedOption设置为1.像这样:

export class TestComponent implements OnInit{
  signUpDetailsForm: FormGroup;
  public submitted : boolean;

  public selectedOption : string ='1';

我可以看到selectedOption值设置正确,如在ngOnInit中,值打印为1:

ngOnInit() {

 console.log('selectedOption='+this.selectedOption);

}

我还尝试了另一种解决方法,方法是在ng-init中设置selectedOption,但仍然不起作用。

<div ng-init="selectedOption=1">
                <div>
                    <input type="radio" value="1" [formControl]="signUpDetailsForm.controls['details']" name="details" (click)="EmailClicked()"
                    ng-model="selectedOption"> Email</div>

                <div>
                    <input type="radio" value="2" ng-model="selectedOption" [formControl]="signUpDetailsForm.controls['details']" name="details" (click)="PhoneClicked()"> Text</div>
                <div>
                    <input type="radio" value="3" ng-model="selectedOption" [formControl]="signUpDetailsForm.controls['details']" name="details" (click)="BothClicked()"> Both</div>

</div>

更新

我的更新代码如下所示,默认情况下仍然没有选择广播:

<div class="form-group">
            <div class="form-text">Some Question about Email and Phone Details?</div>


            <div>
                <input type="radio" value="1" [formControl]="signUpDetailsForm.controls['details']" name="details" (click)="EmailClicked()"
                [(ngModel)]="selectedOption"> Email</div>

            <div>
                <input type="radio" value="2" ng-model="selectedOption" [formControl]="signUpDetailsForm.controls['details']" name="details" (click)="PhoneClicked()"> Text</div>
            <div>
                <input type="radio" value="3" ng-model="selectedOption" [formControl]="signUpDetailsForm.controls['details']" name="details" (click)="BothClicked()"> Both</div>


        </div>

//这是在component.ts类中:

public selectedOption :string;
ngOnInit() {

this.selectedOption="1";

}

1 个答案:

答案 0 :(得分:6)

从威廉的评论中得到了暗示。回答贴在这里:

     //The code for memberPayment collection

MemberProfiles = new Mongo.Collection('memberProfiles');


RecipeSchema = new SimpleSchema({
name: {
    type: String,
    label: "Name"
},
desc: {
    type: String,
    label: "Description"
},
payments:{
    type: [PaymentSchema],
    autoValue: function () {
        return Payments.find({ memberId="uniqueId"});
    },

     defaultValue: function () {
         return Payments.find({memberId="uniqueId"});
    },

},

// The code for payments collection

 PaymentSchema = new SimpleSchema({
name:{
    type: String

},
amount:{
    type: String
},
memberId:{
    type: String
},


});