AngularJs:如何在文本框中输入文本后按Tab键按钮调用angularjs函数?

时间:2016-11-14 13:44:21

标签: angularjs angularjs-controller

我想知道如何通过按Tab键调用angularjs函数?



var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    
    
  
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">

First Name: <input type="text" ng-model="firstName"><br>

</div>
&#13;
&#13;
&#13;

如何在按下firstName并单击制表符按钮后使用angularJs调用该函数。因为我是angularJs的新手,我知道ng-click,ng-change,但我对此一无所知。

请某人帮助我。

提前致谢

2 个答案:

答案 0 :(得分:4)

您可以使用ngBlur

但是,如果您单击输入外部以及标签,它将运行该功能。

&#13;
&#13;
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
      
    $scope.tabbedout = function(val) {
      console.log("tabbedout: " + val);
    }
  
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">

First Name: <input type="text" ng-model="firstName" ng-blur="tabbedout(firstName)"><br>
Last Name: <input type="text" ng-model="lastName" ng-blur="tabbedout(lastName)"><br>

</div>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

你说单击一个按钮,但我假设你想要处理tab keydown / keyup事件,因为没有要点击的标签按钮。

int main
{
    string English;
    ifstream infile("English.txt");
    if (!infile.is_open())  
    {    
        cout << "Cannot open file.\n";    
        exit(1); 
    }

    while (!infile.eof())
    {
      getline (infile,English);
    }
    infile.close();
    cout<<endl;
    cout<<"This is the text in the file:"<<endl<<endl;
    cout<<English<<endl<<endl;

    ofstream codefile("codefile.txt");
    ofstream outfile ("compressed.txt");
    ifstream codefile_input("codefile.txt");
    char ch;
    string st;

    for (int i=0; i<English.length();)
    {
        while(!codefile_input.eof())
        {
            codefile_input >> ch >> st;
            if (English[i] == ch)
            {
                outfile<<st;
                cout<<st;
                i++;
            }
        }
    }
    return 0;
}
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    var _this = this;
    _this.firstName = '';
    _this.handleKeyDown = function($event) {
        if ($event.which == 9)
            alert('tab was pressed! Current value is: ' + _this.firstName);
    }

});