0

Obviously this is caused by me being new to AngularJS, but I don't know what is the problem.

Basically, I have a list of items and an input control for filtering the list that is located in a pop out side drawer.
That works perfectly until I added a directive to set focus to that input control when it becomes visible. Then the focus works, but the filter stops working. No errors. Removing focus="{{open}}" from the markup makes the filter work.

The focus method was taken from this StackOverflow post: How to set focus on input field?

Here is the code...

/* impersonate.html */
<section class="impersonate">
    <div header></div>
    <ul>
        <li ng-repeat="item in items | filter:search">{{item.name}}</li>
    </ul>
    <div class="handle handle-right icon-search" tap="toggle()"></div>
    <div class="drawer drawer-right" 
         ng-class="{expanded: open, collapsed: !open}">
        Search<br />
        <input class="SearchBox" ng-model="search.name" 
               focus="{{open}}" type="text">
    </div>
</section>


// impersonateController.js
angular
    .module('sales')
    .controller(
        'ImpersonateController',
        [
            '$scope',
            function($scope) {
                $scope.open = false;
                $scope.toggle = function () {
                    $scope.open = !$scope.open;
                }
        }]
    );

// app.js
angular
    .module('myApp')
    .directive('focus', function($timeout) {
        return {
            scope: { trigger: '@focus' },
            link: function(scope, element) {
                scope.$watch('trigger', function(value) {
                    if(value === "true") { 
                        console.log('trigger',value);
                        $timeout(function() {
                            element[0].focus(); 
                        });
                    }
                });
            }
        };
    })

Any assistance would be greatly appreciated!

Thanks! Thad

1
  • Could you provide plnkr? plnkr.co/edit Under "New" you can start with angular already integrated. Commented Jul 16, 2013 at 14:50

1 Answer 1

3

The focus directive uses an isolated scope.

scope: { trigger: '@focus' },

So, by adding the directive to the input-tag, ng-model="search.name" no longer points to ImpersonateController but to this new isolated scope.

Instead try:

ng-model="$parent.search.name"

demo: http://jsbin.com/ogexem/3/


P.s.: next time, please try to post copyable code. I had to make quite a lot of assumptions of how all this should be wired up.

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

1 Comment

Thank you, @Yoshi! This worked as advertised! I apologize for the code being in a bad format. I will use plunker from now on as @Narretz suggested.

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.