gpt4 book ai didi

AngularJS:如何在表单字段获得焦点时触发函数调用

转载 作者:行者123 更新时间:2023-12-04 00:08:20 25 4
gpt4 key购买 nike

我希望我的 Controller 在用户单击表单域时(当表单域获得焦点时)执行一些操作。我怎样才能做到这一点?见 this plunker .

这是我的 Controller :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
}
}
]);

这是 View :

<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>

最佳答案

您可以使用 ngFocus 指令。反之为 ngBlur

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

<div ng-app>

<input type="text" ng-focus="isFocused = true" ng-blur="isFocused = false">
<p ng-if="isFocused">Focus !</p>

</div>

关于AngularJS:如何在表单字段获得焦点时触发函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26038606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com