-3

Below is the code:

HTML

<form ng-submit="fun()">
  <select ng-model="a" ng-options="table.id as table.name for table in tables></select>
  <input type="text" ng-model="b">hello</input>
  <input type="submit">Ok</input>
</form>

JS

$scope.tables = [{"id":1, "name":"x"},{"id":2, "name":"y"},{"id":3, "name":"z"}]; 
//This is dummy data, tables is actually coming from another custom API
$scope.fun = function(){
  console.log($scope.a);
  console.log($scope.b);
}

This is the output:

undefined
hello

When I checked the source code, the options getting appended to my select look like this:

<option label="x" value="undefined:undefined"></option>

How can I get the selected option in the controller on ng-submit?

Piyush Shrivastava
  • 1,046
  • 2
  • 16
  • 43

2 Answers2

1

check this....as you changed your data i updated with that

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

app.controller('MainCtrl', function($scope) {
 $scope.tables = [{"id":1, "name":"x"},{"id":2, "name":"y"},{"id":3, "name":"z"}];
 $scope.fun = function(){
  console.log($scope.a);
  console.log($scope.b);
}
});
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <p>Hello {{name}}!</p>
    <form ng-submit="fun()">
  <select ng-model="a" ng-options="table.id as table.name for table in tables"></select>
  <input type="text" ng-model="b">
  <input type="submit">
</form>
  </body>

</html>
0

since you are accessing table.id and table.name in the ng-options you need to change your array to object array like this

$scope.tables = [{"id":"1","name":"1"},{"id":"2","name":"2"},{"id":"3","name":"3"}];

angular.module("app",[])
.controller("ctrl",function($scope){

$scope.tables = [{"id":"1","name":"1"},{"id":"2","name":"2"},{"id":"3","name":"3"}];
$scope.fun = function(){
  console.log($scope.a);
  console.log($scope.b);
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 <form ng-submit="fun()">
  <select ng-model="a" ng-options="table.id as table.name for table in tables"></select>
  <input type="text" ng-model="b" />
  <input type="submit"/>Ok
</form>
</div>

If you don't want to change the array then simply change the ng-options to this

ng-options="table as table for table in tables"

Demo

angular.module("app",[])
.controller("ctrl",function($scope){

$scope.tables = ["1","2","3"];
$scope.fun = function(){
  console.log($scope.a);
  console.log($scope.b);
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 <form ng-submit="fun()">
  <select ng-model="a" ng-options="table as table for table in tables"></select>
  <input type="text" ng-model="b" />
  <input type="submit"/>Ok
</form>
</div>
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80