2016-06-29 6 views
1

Probleme einstellen Löschen EingabefeldProbleme einstellen Löschen Eingabefeld AngularJS

ich zur Zeit versucht, das Eingabefeld auf ng-Modell = „newInstruction.instructionText“, nachdem ein neuer Befehl Text hinzugefügt wurde zu löschen. Hier ist der Code, wo ich versuchte, es auf eine leere Zeichenfolge zurückzusetzen, aber das Eingabefeld wurde nicht gelöscht. Warum das?

Ich habe versucht Konsole die folgenden in updateInstructionText Funktion Protokollierung:

console.log (newInstruction) gibt undefined in der Konsole.

Console.log ($ scope.newInstruction) gibt undefined in der Konsole zurück.

console.log (Befehl) gibt ein Objekt mit dem Befehl Text innerhalb ex: Object {instructionText: 'neue Anweisung'}

Controller:

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> 

Antwort

1

Einfach tun:

instruction.instructionText = null 

Da Sie das Argument (Objekt) innerhalb der Funktion mit dem Namen Anweisung zugreifen, wenn Sie diese auf null gesetzt, können Sie das Eingabefeld löschen

+0

Ich habe TypeError: Kann nicht die Eigenschaft 'AnweisungText' von undefined in der Konsole – Jason

+0

Überprüfen Sie meine Bearbeitung @Jason –

+0

hat es funktioniert! genial! Danke. – Jason

0

einfach tun $ scope.newInstruction.instructionText = null