简体   繁体   中英

reseting input field after entering data

i have one input box, and im working with scanner that automatic inputs numbers when scans something, now i want to automatically assign that value to some variable, and delete it so it can get another input, any ideas?

html

<ion-view hide-nav-bar="true">
  <ion-content class="padding"><br>

    <label class="item item-input">  
      <input type="number" ng-model="code" id="code" name="theInput" auto-focus>
    </label>

    <div class="tt"><br><br>
        Code   : <span class="art">{{code}}<br><br></span>
    </div><br>

    <button ng-click="clear(code)" class="button button-positive">
     Clear
    </button>
  </ion-content>
</ion-view>

js

 .controller('PriCtrl', function($scope) {

    window.onload = function() {
        document.getElementById("code").focus();
    };


    $scope.clear= function(code){

    $scope.val = code;

    document.getElementById("code").value = '';

}

just one line change

$scope.clear= function(code){   
 $scope.val = code;
 $scope.code = ''; //ng-model of input is code
}

you should read about how angular data binding is working

If a variable is in the scope the view can access it. if you modify the variable value the in the controller side the view will be updated automatically and vice versa.

You should avoid using jQuery as much as you can and manipulating DOM from controller like this :

`document.getElementById("code").value = '';`

is strictly the same as $scope.code = '';


this is a plunker : http://plnkr.co/edit/u3loqpxYIBMX65O9FXGD?p=preview

i will create a array to store the input

js :

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

app.controller('MainCtrl', function($scope) {

  $scope.selected = [] ;
  $scope.code = null ;
  $scope.next = function(){
    $scope.selected.push( $scope.code );
    $scope.code = null

  }
});

HTML :

<body ng-controller="MainCtrl">

 <ion-view hide-nav-bar="true">
  <ion-content class="padding"><br>

    <label class="item item-input">  
      <input type="number" ng-model="code" id="code" name="theInput" auto-focus>
    </label>

    <div class="tt"><br><br>
        Code   : <span class="art">{{code}}<br><br></span>
    </div><br>

    <button ng-click="next()" class="button button-positive">
      scan next
    </button>
  </ion-content>
</ion-view> 

<pre>{{selected|json}}</pre>


</body>

In Angular, try to never do something like this document.getElementById("code").value = ''; .

You could simply watch the code variable for changes, and if it gets a new value, you copy it over into a values list and then delete the value from code .

.controller('PriCtrl', function($scope) {
    $scope.$watch('code', function(newVal, oldVal) {
        if (newVal != '') {
            $scope.codelist.push(newVal);
            $scope.code = '';
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM