0

I have a problem using <select> with ng-repeat in Angular.

I'm using the following code:

HTML:

<select ng-model="device.remote" ng-repeat="remoteID in device.remoteIDs">
    <option value="{{ remoteID.id }}">{{ remoteID.name }}</option>
</select>

Angular:

$scope.device = response.data;

response.data looks like this:

{
    "remote": "",
    "remoteIDs": [
        {
            "id": "1",
            "name": "TEST"
        }
    ]
}

In the WebApp I get this result:

<option value="? string: ?"></option>
<option value="1" class="ng-binding">TEST</option>

but I dont want to show the ? string: ?-option. How can I remove it?

I only want to display the options in remoteIDs

3
  • will be easy to help you if u share $scope.device data Commented Apr 18, 2017 at 12:18
  • $scope.device is exactly the same as response.data Commented Apr 18, 2017 at 12:20
  • Try use ng-options instead of ng-repeat Commented Apr 18, 2017 at 12:22

4 Answers 4

2

You need to give a default option to the users.

You can do this by simply adding:

<option value="" disabled="true">Please Select</option>

See the example in code.

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

app.controller('ctrl', function($scope) {
  $scope.device = {
      "remote": "",
      "remoteIDs": [
          {
              "id": "1",
              "name": "TEST"
          }
      ]
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.css" />

<body ng-app="app" ng-controller="ctrl">
  <div class="col-lg-12">
    <select ng-model="device.remote" class="mySelect">
      <option value="" disabled="true">Please Select</option>
      <option value="{{ remoteID.id }}" ng-repeat="remoteID in device.remoteIDs">{{ remoteID.name }}           </option>
  </select>
  </div>
</body>

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

4 Comments

That's it! Thank you very much!
But ,In this case how can we deselect an option that is already selected?I think its better to remove the ng-disabled.
@Mr.niks It depends on the use case. If it is required for the user to be able to deselect an option then remove the "disabled" bit on the default option.
Yes, I removed the disabled="true" and it works just fine
0
<select ng-model="device.remote>
    <option ng-repeat="remoteID in device.remoteIDs" 
        ng-if="remoteID.id" value="{{ remoteID.id }}">{{ remoteID.name }}</option>
</select>

Comments

0

don't understand why are you using ng repeat in the select element. use it in the options

 <select ng-model="device.remote">
        <option  ng-repeat="remoteID in device.remoteIDs" value="{{ remoteID.id }}">{{ remoteID.name }}</option>
    </select>

angular.module("app",[])
.controller("ctrl",function($scope){
$scope.device = {
    "remote": "",
    "remoteIDs": [
        {
            "id": "1",
            "name": "TEST"
        }
    ]
}

})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 <select ng-model="device.remote">
    <option  ng-repeat="remoteID in device.remoteIDs" value="{{ remoteID.id }}">{{ remoteID.name }}</option>
</select>
</div>

Comments

0

If you want to remove that empty option, you should take the first option as default selected. or you can give an empty option without any value please see the snippet,

<!DOCTYPE html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <title>AngularJS Plunker</title>
  <script data-require="[email protected]" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
</head>

<body ng-controller="MainCtrl">
  <select ng-model="device.remote" ng-repeat="remoteID in device.remoteIDs">
    <option value="{{ remoteID.id }}">{{ remoteID.name }}</option>
  </select>
  <select ng-model="device1.remote" ng-repeat="remoteID in device1.remoteIDs">
    <option value="">select</option>
    <option value="{{ remoteID.id }}">{{ remoteID.name }}</option>
  </select>
  <script>
    var app = angular.module('plunker', []);

    app.controller('MainCtrl', function($scope) {


      $scope.device = {
        "remote": "1",
        "remoteIDs": [{
          "id": "1",
          "name": "TEST"
        }]
      };

      $scope.device1 = {
        "remote": "",
        "remoteIDs": [{
          "id": "1",
          "name": "TEST"
        }]
      };


    });
  </script>
</body>

</html>

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.