Angularjs 在文本框中按任意键时如何调用控制器方法?
我对angularjs很陌生 我想在angularjs的文本框中输入任何文本时,使用ajax调用调用我的控制器方法 我想在textbox上的任何按键上调用我的控制器方法,或者将其删除Angularjs 在文本框中按任意键时如何调用控制器方法?,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我对angularjs很陌生 我想在angularjs的文本框中输入任何文本时,使用ajax调用调用我的控制器方法 我想在textbox上的任何按键上调用我的控制器方法,或者将其删除 <input type="text" data-ng-model="newfriend.SearchText" required /> 有人能帮我吗?扩展我的评论: <input type="text" data-ng-model="newfriend.SearchText" ng-change
<input type="text" data-ng-model="newfriend.SearchText" required />
有人能帮我吗?扩展我的评论:
<input type="text" data-ng-model="newfriend.SearchText" ng-change="myMethod()" required />
$scope.myMethod = function () {
$http.get('../Home/GetFriendsList').success(function (data) {
$scope.friends = data;
})
.error(function () {
$scope.error = "An Error has occurred while loading posts!";
});
}
或者,调用myMethod函数作为
<input type="text" data-ng-model="newfriend.SearchText" ng-keypress="myMethod(newfriend.SearchText)"
请使用ng change=function()
或$watch()
提供代码..ng keypress指令用于此。
<input type="text" data-ng-model="newfriend.SearchText" required />
$scope.$watch('$scope.newfriend.SearchText', function (newVal,oldVal) {
if (newVal){
$scope.myMethod();
}
})
var text = $scope.newfriend.SearchText
<input type="text" data-ng-model="newfriend.SearchText" ng-keypress="myMethod(newfriend.SearchText)"