1
car.html

    <body ng-app="carService" ng-controller="selectDropdown">
        <div>
            Car Brand:
            <select ng-model="userSelect">
                <option value="">--Select--</option>
                <option ng-repeat="ManufactureBrand in a" ng-bind="ManufactureBrand" 
                </option>
            </select>
            <input type="button" value="submit" ng-click="checkselection()">
            <span color:red>{{msg}}</span>
        </div>
    </body>

detail.js

var app=angular.module('carService',[]);

app.factory('Brandtest',function(){
    var brand={};
    brand.sample=['Bmw','Mercedes','Honda'];
    return brand;
});

app.controller('selectDropdown',['$scope','Brandtest',function($scope,Brandtest){
    $scope.a=Brandtest.sample;

   $scope.checkselection= function(){
        if($scope.userSelect !="" && $scope.userSelect !=undefined){
        $scope.msg = $scope.userSelect;

}
}
}]);

Hi, Im trying angularjs newly and Im having problem in displaying the selected item from a dropdown.Please help how to display the selected item from the drop-down box. Thanks in advance.

3 Answers 3

1

try this, using ng-options:

    <body ng-app="carService" ng-controller="selectDropdown">
            <div>
                Car Brand:
                <select ng-model="userSelect" ng-options=" item as item for item in a">
                    <option value="">--Select--</option>
                </select>
                <input type="button" value="submit" ng-click="checkselection()">
                <span color:red>{{msg}}</span>
            </div>
        </body>

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>

        <script>
            var app=angular.module('carService',[]);

            app.factory('Brandtest',function(){
                var brand={};
                brand.sample=['Bmw','Mercedes','Honda'];
                return brand;
            });

            app.controller('selectDropdown',['$scope','Brandtest',function($scope,Brandtest){
                $scope.a=Brandtest.sample;

               $scope.checkselection= function(){
                    if($scope.userSelect !="" && $scope.userSelect !=undefined){
                    $scope.msg = $scope.userSelect;


            }
            }
            }]);
        </script>
Sign up to request clarification or add additional context in comments.

Comments

0

Use value in option

 <option ng-repeat="ManufactureBrand in a" ng-bind="ManufactureBrand" value="{{ManufactureBrand}}" ></option>

Otherwise you were right

Comments

0

Check this snippet, made few changes in your code.

var app=angular.module('carService',[]);

app.factory('Brandtest',function(){
    var brand={};
    brand.sample=['Bmw','Mercedes','Honda'];
    return brand;
});

app.controller('selectDropdown',['$scope','Brandtest',function($scope,Brandtest){
    $scope.a=Brandtest.sample;
    $scope.checkselection= function(userSelect){
        if($scope.userSelect !="" && $scope.userSelect !=undefined){
        $scope.msg = $scope.userSelect;

}
}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="carService" ng-controller="selectDropdown">
        <div>
            Car Brand:
            <select ng-model="userSelect">
                <option value="">--Select--</option>
                <option value="{{ManufactureBrand}}" ng-repeat="ManufactureBrand in a" ng-bind="ManufactureBrand">
                </option>
            </select>
            <input type="button" value="submit" ng-click="checkselection()">
            <span style="color:red">{{msg}}</span>
        </div>
    </body>

Comments

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.