Как очистить поле ввода при нажатии кнопки с AngularJS? - PullRequest
0 голосов
/ 20 июня 2020

в JavaScript вот так:

<!DOCTYPE html>
<html>
<body>

<p>Clear the input field when you click on the button:</p>
<button onclick="document.getElementById('myInput').value = ''">Clear input field</button>

<input type="text" id="myInput">

</body>
</html>

Как использовать с AngularJS?

Ответы [ 2 ]

0 голосов
/ 20 июля 2020

я преуспел в dom javascript со встроенным событием onclick

<input type="text" id="hdd_data_2" value="3 TB">
<button type="button" onclick="document.getElementById('hdd_data_2').value ''">Clear</button>
0 голосов
/ 23 июня 2020

Вы можете решить эту проблему с помощью директивы ng-model, вот пример того, как вы можете реализовать ее в своем коде

index. html

<div ng-controller="MainController">
  <p>Clear the input field when you click on the button:</p>
  <button ng-click="clearInput()">Clear input field</button>

  <input type="text" id="myInput" ng-model="input">
</div>

app. js

var myApp = angular.module('myApp',[]);

myApp.controller('MainController', ['$scope', function($scope) {
  $scope.input = '';

  $scope.clearInput = function() {
     $scope.input = '';
  }
}]);

Дополнительные примеры см. В документации angularjs. https://docs.angularjs.org/guide/controller

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...