I want to call an event when focusing each element.
However I think it is not good to set an ngFocus directive on each element as there can be many of them.
How can I add an on focus event handler to each element at runtime?
<ng-form name="myForm">
<p><label>name1: </label><input type="text" name="name1" ng-focus="onFieldFocus(myForm, 'name1')"/></p>
<p><label>name2: </label><input type="text" name="name2" ng-focus="onFieldFocus(myForm, 'name2')"/></p>
</ng-form>
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name1 = 'text1 here';
$scope.name2 = 'text2 here';
$scope.result = "Nothing";
$scope.onFieldFocus = function (_formCtrl, _fieldName) {
$scope.result = "field "+_fieldName+" of form "+_formCtrl.$name+" was focused";
}
});
I would like to use angular.element
and $scope.myForm
with a a for..each through myForm
elements not starting with $
, something similar to this:
for (var i in $scope.myForm) {
console.log(i);
$scope.result += "1";
if ($scope.myForm.hasOwnProperty(i)) {
var property = $scope.myForm.i;
if(property.charAt(0) != '$') {
$scope.result += property+", ";
}
}
}