2016-12-01 3 views
0

を使用して、フォームの入力をリセットすることはできませんそして、ここで入力値

をリセットし、私のHTMLは

<form ng-controller="questionsCTRL" class="ui large form" name="questionForm" ng-submit="addSurvey(questionForm)" novalidate> 

<div class="ui segment" id="quest-answers"> 


    <div class="two fields"> 
    <div class="field"> 
     <label>Add New Answer</label> 
     <div class="ui action input"> 
     <input type="text" required name="answers" ng-model="answers" 
     ng-required="true" ng-minlength="5" placeholder="answer..."> 
     <button type="button" 
     ng-click="addAnswer(questionForm.answers.$viewValue)" 
     ng-disabled="questionForm.answers.$invalid" 
     class="ui teal right labeled icon button"> 
     <i class="add icon"></i> 
     Add 
     </button> 
    </div> 
    <small ng-show="questionForm.answers.$invalid" class="ui meta teal">Answers is required</small> 
    </div> 
</div> 
<div class="ui grid"> 
    <div class="row"> 
    <div class="eleven wide column"> 

     <div class="field"> 
      <div class="ui attached segment" ng-repeat="answer in answerGroup"> 
      {{answer.text}} 
      <a href class="ui right floated link"><i class="circular delete icon"></i></a> 
      </div> 
     </div> 

    </div> 
    </div> 
</div> 
</div> 
</form> 
です

これはコントローラコンテンツである

UIASSIGN1.controller('questionsCTRL', function($scope , $rootScope , $state, $stateParams , $http) { 
      // #dummy controller 
     $rootScope.sectorName  = 'Questions'; 
     var _SID     = $stateParams.id; 
     $scope.answerGroup   = []; 

     $http.get("api/survey/survey.json") 
     .then(function(response) { 
     var surveyArray = response.data; 
     $scope.surveyArray = []; 
     for (var i = 0; i < surveyArray.length; i++) { 
      var thisItem = surveyArray[i]; 
      var thisElm = {name:thisItem.name,value:parseInt(thisItem._id)}; 
      $scope.surveyArray.push(thisElm) 
     } 

     }); 

     $scope.addAnswer = function(answer){ 
      var inArray = { 
      text : answer 
      }; 
      $scope.answerGroup.push(inArray); 
      $scope.questionForm.answers = {}; 
     } 
}); 

このNGクリックは$ scope.userGroupsに値を加算するが、それは、フォームの入力値のみ{}

0から$ scope.questionForm.answers値をリセットするリセットしません
+0

あなたのHTMLに '$ scope.questionForm.answers'の値をどこに表示しているのかわかりません。おそらくあなたは '$ scope.answerGroup'や' $ scope.answers'を変更するつもりですか? –

+0

'入力タイプ="テキスト "必須の名前="回答 "ng-model ="回答 " ng-required =" true "ng-minlength =" 5 "プレースホルダー="回答... ">'入力ここで '$ scope.questionForm.answers'の値は –

+0

から来ます。次に' $ scope.answers = "" 'を設定します。 –

答えて

0

私は$scope.answerの値がこの要素にマッピングされているコメントで述べたように:

<input type="text" required name="answers" ng-model="answers" 
    ng-required="true" ng-minlength="5" placeholder="answer..."> 

ng-model="answers"は、範囲内にanswersという変数を定義することを意味します。

$scope.answers = ""; 

は、入力要素をリセットします:次に角度にあなたは$scope.answersとそれにアクセスすることができ、それが理由です。

0

このようにすることができます。入力フィールドのためにあなたのngModelとしてスコープに

$scope.theAnswer = ''; 

設定し、これを答えを定義します。

ng-model="theAnswer" 

があなたの簡素化、このようにNGをクリックして、直接、コントローラで、その後、アレイに答えを追加クリーンアップの答え:

ng-click="addAnswer()" 

$scope.addAnswer = function(){ 
     var inArray = { 
     text : $scope.theAnswer 
     }; 
     $scope.answerGroup.push(inArray); 
     $scope.theAnswer = ''; 
    } 
関連する問題