AngularJS radio button group with “other” option that includes a text field

后端 未结 2 788
萌比男神i
萌比男神i 2021-01-25 05:29

I have a form with three radio buttons in a group. The third radio button is \"Other\" and has a text field where the user can enter something in. I can make the radio button r

2条回答
  •  Happy的楠姐
    2021-01-25 05:42

    You can use ng-required. Something like

    
    

    should work.

    Regarding your other issue, you have to employ some controller-logic and some kind of temporary variable for form.OtherProgram, using $watch for example.

    $scope.$watch('form.Program', function(mVal){
      if (angular.isUndefined($scope.form)) return; 
    
      if(mVal === 'other'){
         $scope.form.OtherProgram = $scope.tmVar;
      } else {
        if($scope.form.OtherProgram !== null){
          $scope.tmVar = $scope.form.OtherProgram;
          $scope.form.OtherProgram = null;
       }
     }
    });
    

    Plunker: http://plnkr.co/edit/npvUXpRhB5MYJOstwd88?p=preview

提交回复
热议问题