2014-12-19 2 views
1

저는 angularjs에서 매우 새로 왔습니다.텍스트 상자에서 키를 누르면 컨트롤러 메서드를 호출하는 방법?

anglejs의 텍스트 상자에 텍스트를 입력 할 때 ajax 호출을 사용하여 컨트롤러 메서드를 호출하고 싶습니다.

텍스트 상자의 키 누르기에서 내 컨트롤러 메서드를 호출하거나 제거하려고합니다.

$http.get('../Home/GetFriendsList').success(function (data) { 
     $scope.friends = data; 

    }) 
    .error(function() { 
     $scope.error = "An Error has occured while loading posts!"; 
    }); 

이 사람이 나를 도울 수

??? : 내가 해고하려면이 텍스트 상자에 아무 키나 눌러에

<input type="text" data-ng-model="newfriend.SearchText" required /> 

이 전화 텍스트 상자에 업데이트 된 값으로 MyMethod라는 함수를 호출하는

<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!"; 
    }); 
} 

또 다른 방법 :

+0

$watch() 코드 ... – Madhu

+1

NG-키를 누를 때 지침이 거기에 제발 제공 사용할 때, MyMethod라는 함수를 호출합니다. – Ved

답변

0

내 댓글을 확장.

<input type="text" data-ng-model="newfriend.SearchText" required /> 

$scope.$watch('$scope.newfriend.SearchText', function (newVal,oldVal) { 
       if (newVal){ 
        $scope.myMethod(); 
        } 
       }) 

이미 ng-model을 사용하고 있습니다. 컨트롤러에서 그 값을 얻을 수 있습니다. 그냥,

var text = $scope.newfriend.SearchText 

를 사용하거나

<input type="text" data-ng-model="newfriend.SearchText" ng-keypress="myMethod(newfriend.SearchText)" 
+0

http://chat.stackoverflow.com/rooms/67291/discussion-between-ved-and-maria-pithia – Ved

+0

나는 당신의 코드를 확인했다. – Ved

0

하나가 ng-change=function() 또는

관련 문제