1

Trying to capture the focus or click event of a user returning to a search field.

<form name="searchUser">
    <input type="search" placeHolder="Username to find" ng-model="username" ng-focus="focusSet()">
    <input type="submit" value="Search" ng-click="search(username)" />
</form>

When someone click on the text(search) field I want to start a timer. The timer works for the initial page refresh but I cant get it to work for ng-focus or ng-click. So I degenerated to a simple focusSet function that just writes a line to the console. But it never writes the line on focus. Also tried this with click.

Why does angular not respond to the focus event?

var focusSet = function(){
  $log.info("Got Focus");
  $scope.focusSet = "now you see me";

};

here is my plunker if you want to see the whole thing http://plnkr.co/edit/zmWVWJrigRYkQk5jS5HU?p=preview

1 Answer 1

1

try:

$scope.focusSet = function(){
  $log.info("Got Focus");
  // $scope.focusSet = "now you see me";
};
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this. Obviously you cant set a scope.focusSet inside of scope.focusSet function, but after updating the variable it worked. Im new to angular, can you explain why this worked vs my implementation?
This kind of helps stackoverflow.com/questions/24287954/scope-vs-var-in-angularjs, but im still a little fuzzy on how it relates to var functions.
any attribute name you define in ng-* such as ng-model, ng-click, ng-focus belongs to the controller scope, that's why you have to declare $scope.focusSet

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.