简体   繁体   English

如何实时检查输入字段是否与angular.js匹配另一个变量

[英]How to check in real time if a input field matches another variable with angular.js

I have a ng-repeat that loops through an object and displays a list of strings and input fields, example below. 我有一个ng-repeat,它遍历一个对象并显示字符串和输入字段的列表,例如下面的示例。 I need to set up a real-time verification for these input fields. 我需要为这些输入字段设置实时验证。 The input field should be equal to the barcode string. 输入字段应等于条形码字符串。 If a user types in a string that IS NOT equal to the barcode string than angular should prompt the user that it must equal the barcode string and clear the input field. 如果用户输入的字符串与条形码字符串不相等,则输入的角度不等于角度,应提示用户必须与条形码字符串相等,并清除输入字段。 Is this possible with angular? 角度可以吗? I've accomplished similar tasks with jQuery.. could I combine some jQuery and Angular to achieve this verification? 我已经用jQuery完成了类似的任务。我可以结合使用jQuery和Angular来完成此验证吗? I'm new with angular.js so any help is greatly appreciated. 我是angular.js的新手,因此非常感谢您的帮助。

HTML: HTML:

<div ng-repeat="(index, val) in barcodes.barcodes track by $index">
  <div class="form-group row" ng-show="barcodes.barcodes[index]">
    <label class="col-sm-3 form-control-label" style="margin-top: 5px"> {{ barcodes.adaptors[$index] }} </label>
    <label class="col-sm-3 form-control-label" style="margin-top: 5px"> {{ barcodes.barcodes[index] }} </label>
    <div class="col-sm-6">
      <input type="email" class="form-control" placeholder="Barcode">
    </div>
  </div>
</div>

Example bars Object: 示例栏对象:

var bars = {
  "adaptors": ["506-704", "505-703", "503-702", "508-701", "507-705", "502-706", "504-707", "501-708"],
  "barcodes": ["11-11-1111","11-11-2222","11-11-3333","11-11-4444","X","X","X","X"];
}

在此处输入图片说明

if you want real time...watcher may be what you're looking for. 如果您想要实时...观察者可能就是您想要的。 But watcher is not efficient. 但是观察者效率不高。

What would be best is to use the ng-change directive. 最好是使用ng-change指令。 This way, every time a user types, it will trigger the function passed to the ng-change directive. 这样,用户每次键入时,都会触发传递给ng-change指令的函数。 This can allow you to handle logic inside a function and react however you want. 这可以让您处理函数内部的逻辑并根据需要进行响应。

https://docs.angularjs.org/api/ng/directive/ngChange https://docs.angularjs.org/api/ng/directive/ng更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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