2

I have an input field which is for both setting and readback. I would like to have the following behavior:

  • No focus: update the field with readback
  • Focus: no update, if enter is pressed call send with the value written in the field.

I have a mwe which is a start but does update when focused.

<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.js"></script>
<script>
    angular.module('App', []).controller('world', function ($scope) {
		$scope.value1 = 0;
		$scope.value2 = 0;
        $scope.send = function(value) {
            alert(value);
        }

        let readback = function() {
			$scope.value1 += 1;
			$scope.value2 += 1;
			$scope.$apply();
			setTimeout(readback, 100);
        };
        setTimeout(readback, 1);
    });
</script>
</head>

<body ng-app="App" ng-controller="world">
<input type="text" ng-model="value1" ng-keyup="$event.keyCode == 13 && send(value1)" />
<input type="text" ng-model="value2" ng-keyup="$event.keyCode == 13 && send(value2)" />
</body>

Edit: Sorry I was a bit unclear and cut down my mwe too much. I have about ten such input fields. The unfocused fields should still update while only the one which is selected does not.

1 Answer 1

1

you can detect ng-focus and ng-blur, using separate controller instances for each input:

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.js">
    </script>
    <script>
        angular.module('App', []).controller('world', function ($scope) {
            let isFocus = false;
            
            $scope.value = 0;
            $scope.send = () => alert($scope.value);
            $scope.focus = () => isFocus = true;
            $scope.blur = () => isFocus = false;
            
            let readback = function() {
                if (!isFocus) {
                  $scope.value += 1;
                  $scope.$apply();
                }
                setTimeout(readback, 100);
            };
            setTimeout(readback, 1);
        });
    </script>
</head>

<body ng-app="App">
    <input type="text"
           ng-controller="world"
           ng-model="value"
           ng-keyup="$event.keyCode == 13 && send()"
           ng-focus="focus()"
           ng-blur="blur()" />
    <input type="text"
           ng-controller="world"
           ng-model="value"
           ng-keyup="$event.keyCode == 13 && send()"
           ng-focus="focus()"
           ng-blur="blur()" />
</body>

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry I was a bit unclear and cut down my mwe too much. I have about ten such input fields. The unfocused fields should still update while only the one which is selected does not.
updated to use separate controller instance for each input
That will be difficult as all the data comes in one structure and the fields are created with a ng-repeat. The solution I could see would be to make a isFocused for each individual field. I'll accept your answer later tonight if nothing more elegant shows up. Meanwhile have my +1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.