2016-06-29 9 views
1

トラブルの設定私は現在、新しい命令テキストが追加された後にNG-モデル=「newInstruction.instructionText」上の入力ボックスをクリアしようとしている苦労設定クリア入力フィールドAngularJS

クリア入力フィールドを持ちます。ここで私は空の文字列にそれをリセットしようとしたが、入力フィールドがクリアされていないコードです。何故ですか?私はupdateInstructionText関数の中で、次のログインコンソールを試してみた

にconsole.log(newInstruction)は、コンソールでundefinedを返します。

Console.log($ scope.newInstruction)は、コンソールで未定義を返します。オブジェクト{instructionText: '新しい命令'}

CONSOLE.LOG(命令)EX内部命令テキストとオブジェクトを返す

コントローラー:

angular.module('app').controller('InstructionController', function($scope, $http, NgTableParams, $filter) { 
    $scope.initList = function() { 
    $scope.getRemoteInstructions = function(typed) { 
     $http.get("/api/instructions?searchInstructionText=" + typed).then(function(response) { 
     $scope.instructionChoices = _.map(response.data, function(instruction) { 
      instruction.instructionText; 
     }); 
     }); 
    }; 
    $scope.updateInstructionText = function(instruction) { 
     var instructionPromise; 
     if (instruction.id) { 
     instructionPromise = $http.put("api/instructions/" + instruction.id, instruction); 
     } 
     else { 
     instructionPromise = $http.post("/api/instructions", instruction); 
     } 

     $scope.newInstruction = ''; 
     $instruction = ''; 

     instructionPromise.then(function(response) { 
     $('#instructionModal').closeModal(); 
     $scope.instructionTable.reload(); 
     }); 
    }; 
    }; 
}); 

HTML

<div class="container-fluid" ng-init="initList()"> 
    <div class="row"> 
     <h3>Total Instructions: {{totalInstructions}}</h3> 
     <table class="striped highlight bordered" ng-table="instructionTable" show-filter="true"> 
     <tr> 
      <td class="input-field"> 
      <input placeholder="Enter New Instruction Text" ng-model="newInstruction.instructionText" type="text"> 
      </td> 
      <td> 
      <a class="waves-effect waves-light btn" ng-click="updateInstructionText(newInstruction)">Add</a> 
      </td> 
     </tr> 
     </table> 
    </div> 
    </div> 
    <ng-include src="'/views/modals/instructionModal.html'"></ng-include> 
</div> 

答えて

1

単純に:

instruction.instructionText = null 

名前命令で関数内で引数(オブジェクト)にアクセスしているので、あなたがnullに設定している場合、あなたは、入力フィールドをクリアすることができ

+0

私はTypeErrorを持っています:コンソールで未定義のプロパティ 'instructionText'を設定できません – Jason

+0

私の編集を確認してください@Jason –

+0

それは働いた!驚くばかり!ありがとうございました。 – Jason

0

は単に $ scope.newInstruction.instructionText =ヌルを行います