Nivas Dhina Nivas Dhina - 3 months ago 6
Node.js Question

my option input is not working in angularjs

I am building my first angular web application iam trying to deploy one form and i couldn't get the value of the select option to my database i used the below code for getting my value to the database.

<div class="form-group">
<label class="control-label" for="basicSelect">Total Hours of Test</label>
<label class="select select-o">
<select id="basicSelect" name="basicSelect" ng-model="test.totalTime">
<option value="">- Select One -</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="2.5">2.5</option>


</select>
</label>
</div>


controller code

$scope.test = {

totalTime: null,
testType: null

};


i use mongodb at database and node at the backend.

Other parts of the form is getting through clearly and in full shape. but i cant get this value alone. am i going wrong in the html part. Could anyone sort me out.

Answer

I think that what you want to do is call a function when the value is changed you can use ng-change to call a function whenever the value is changed like this:

    <!DOCTYPE html>
    <html>
      <head>
        <script data-require="angularjs@1.5.8" data-semver="1.5.8" src="https://opensource.keycdn.com/angularjs/1.5.8/angular.min.js"></script>
        <link rel="stylesheet" href="style.css" />
        <script>
          var app = angular.module('yourApp', []);
          app.controller('YourCtrl', function($scope) {
            $scope.test = {
              totalTime: null,
              testType: null
            };
            $scope.selectedTime = function(value) {
              alert('selectTime ' + value);
            };
          });
        </script>
      </head>
      <body ng-app="yourApp">
        <div ng-controller="YourCtrl">
          <div>
            test.totalTime: {{test.totalTime}}
          </div>
          <div class="form-group">
            <label class="control-label" for="basicSelect">Total Hours of Test</label>
            <label class="select select-o">
              <select id="basicSelect" name="basicSelect" ng-model="test.totalTime" ng-change="selectedTime(test.totalTime)">
                <option value="">- Select One -</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="2.5">2.5</option>
              </select>
            </label>
          </div>
        </div>
      </body>
    </html>

I have created a showing you my idea plunker