Angular: 如何使用 $this 获取输入值

Angular: How to get an input value using $this

我希望在可以从多个输入运行的 keyup 函数中获取输入值。每次有一个 keyup 函数都会根据特定的输入触发。所以,我试图在函数内部使用 $this 。到目前为止没有成功.. HTML代码:

<input ng-keyup="getRxcui()" placeholder="Type med a" id="medicineA" />
<input ng-keyup="getRxcui()" placeholder="Type med b" id="medicineB" />

Angular代码:

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

rxConflicts.controller('MainController', function($scope, $http){
    $scope.getRxcui = function(event){

        // getting the value for each medicine input
        var medValue = $(this).value;
        console.log(medValue);

    }
});

我很确定 $(this) 是执行此操作的正确方法,因此我不需要为每个输入复制该函数并使用 ng-model...你可以相信我的话 angular 工作正常。 任何帮助表示赞赏。谢谢

使用 ng-model 并在函数中传递它:

<input ng-keyup="getRxcui(medicineA)" ng-model="medicineA" placeholder="Type med a" id="medicineA" />
<input ng-keyup="getRxcui(medicineB)" ng-model="medicineB" placeholder="Type med b" id="medicineB" />

Angular代码:

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

rxConflicts.controller('MainController', function($scope, $http){
    $scope.getRxcui = function(value){

        // getting the value for each medicine input
        var medValue = value;
        console.log(medValue);

    }
});

Angular2及上级:

您可以在 keyUp 上传递 $event 并使用它来获取目标(输入)及其值。如果您使用的是 formControls 而不是直接通过 ngModel

进行绑定

模板HTML:

<input (keyup)="getRxcui($event)">

Component.ts

getRxcui(event){

   var inputValue = event.target.value;
   console.log(inputValue);

}