我正在构建我的第一个角度Web应用程序,我试图部署一个表单,我无法获得我的数据库的选择选项的值我使用下面的代码获取我的数据库的价值。
<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>控制器代码
$scope.test = { totalTime: null, testType: null };我在数据库使用mongodb,在后端使用节点。
表格的其他部分清晰而完整。 但我不能单独得到这个价值。 我在HTML部分出错了。 任何人都可以解雇我。
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.
最满意答案
我认为你想要做的是在值改变时调用函数你可以使用ng-change来调用函数,只要值改变如下:
<!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 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
更多推荐
发布评论