3

i am making dropdown list with input field in angular.js but got no success

the code which i using..

<div ng-app="" ng-controller="namesCtrl">
            <h2>filter input</h2>
            <input type="text" ng-model="test"/>
            <ul>
                <li ng-repeat="x in names | filter:test | orderBy : 'name'">
                    {{ x.name + ',' + x.country }}
                </li>
            </ul>
        </div>

    <script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope) {
        $scope.firstName= "";
        $scope.lastName= "";
    });
</script>
<script src="namescontrol.js"></script>
3
  • 1
    What do you mean by dropdown list with input field? You are showing list items in your code ,not dropdown list.Can u share a fiddle or plunker? Commented Aug 11, 2015 at 14:16
  • i want that, when i type any letter in input field then the dropdown list will be appear of related letter, i mean list should appear with filteration Commented Aug 11, 2015 at 14:19
  • Okay. can u share a plunker or fiddle? Commented Aug 11, 2015 at 14:22

2 Answers 2

1

Check the working demo: JSFiddle.

Use a customized filter to perform the filtering. Since ng-model binds to the value key. Whenever key is changed, the items will be filtered and the view will be changed.

angular.module('Joy',[])
.controller('JoyCtrl', ['$scope', function ($scope) {
    $scope.items = ['one', 'two', 'three', 'four', 'five'];
    $scope.key = '';
    $scope.search = function (value) {
        return value.indexOf($scope.key) >= 0;
    }
}]);

HTML:

<div ng-app="Joy" ng-controller="JoyCtrl">
    <input type="text" ng-model="key">
    <div>
        <li ng-repeat="item in (items | filter:search)" ng-bind="item"></li>
    </div>
</div>

Update 1

If you want to hide the list initially: JSFiddle:

$scope.search = function (value) {
    return $scope.key !== '' && value.indexOf($scope.key) >= 0;
};

Update 2

I have developed an open source project angular-sui based on Angular and Semantic-UI. There is a directive sui-select, which is exactly what you want. Please check the Demo.

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

3 Comments

dropdown list is there already. i want that dropdown list not show before type anything
Thank u for giving answer, joy
@TanveerAhmad My pleasure. Please kindly upvote/accept :)
1

I think what you are looking for is autocomplete functionality. AngularUI offers this through their Typeahead directive. https://angular-ui.github.io/bootstrap/#/typeahead

You want to have something like this:

<input type="text" ng-model="test" typeahead="name for name in names"/>

The directive will dynamically generate the list so you don't need to create that explicitly yourself.

1 Comment

Heres a plunker to get you started: plnkr.co/edit/aUfWBBgxpGjUN6u9eGsN?p=preview

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.