我希望我的控制器在用户单击表单域(当表单域获得焦点时)时执行某些操作。我怎样才能做到这一点?见this plunker。AngularJS:如何在表单域获得焦点时触发函数调用
这里是我的控制器: angular.module( '对myApp',[])
.controller('AppCtrl', ['$scope',
function($scope) {
$scope.field1 = "Default text";
$scope.focus = false;
$scope.formFieldJustGotFocus = function() {
$scope.focus = true;
// Do all the stuff I need to happen when the form has just gotten focus
}
}
]);
这里是视图:
<body ng-app="myApp" ng-controller="AppCtrl">
<h3>Testing</h3>
<p>I want to perform some action in the controller when a form field gets focus. How can I best achieve this?</p>
<form name="myForm">
<input type="text" name="field1" ng-model="field1">
</form>
<p>Form field has focus: {{focus}}</p>
</body>
很多正确答案的,都在aprox的同时给予(我看不出谁是第一个),所以我会奖励你@meriadec对具有应答的演示,我不知道你可以在这里做到这一点在stackoverflow :) – EricC 2014-09-26 05:57:35