javascript - How to get a selected option in AngularJs -
i have spent hours on script, want output text , not value of select option in angularjs in html data binding, when try i'm alway getting value , not text. how can accomplish this. here fiddle
(function(angular) { 'use strict'; angular.module('formexample', []) .controller('examplecontroller', ['$scope', function($scope) { $scope.typeofwritings = [{ value: '5', text: 'writing scratch' }, { value: '3', text: 'editing or proofreading' } ]; $scope.type_writing = $scope.typeofwritings[0].value; $scope.pieces = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; $scope.piece = 1; $scope.calculate = function(){ $scope.calculation = ($scope.piece * $scope.type_writing); } $scope.calculate(); }]); })(window.angular); <!doctype html> <html> <head> <meta charset="utf-8"> <title>return selected input in angular js</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script> <script src="app.js"></script> <body ng-app="formexample"> <div ng-controller="examplecontroller"> <form name="form" novalidate action="formsubmitted.php" method="post"> <table width="465" border="0"> <tbody> <tr> <td width="186"><label for="type_writing">type of writing:</label></td> <td width="269"><select ng-change="calculate()" ng-model="type_writing" ng-options="o.value o.text o in typeofwritings" ng-init="type_writing='5'" name="type_writing" id="type_writing"> </select> </td> </tr> <tr> <td><label for="num_pieces">number of pieces:</label></td> <td><select ng-change="calculate()" ng-model="piece" ng-options="o o o in pieces" name="num_pieces" id="num_pieces"></select></td> </tr> <tr> <td> </td> <td><strong>order cost</strong> <strong>{{type_writing}}</strong> <strong>{{piece}}</strong> pages<div style="color:#d76d25; font-size:24px;">${{calculation}}</div></td> </tr> </tbody> </table> </form> </div> </body> </html>
here solution, should define ng-options
fiddle: https://jsfiddle.net/4zj4a9z7/2/
<title>return selected input in angular js</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script> <script src="app.js"></script> <body ng-app="formexample"> <div ng-controller="examplecontroller"> <form name="form" novalidate action="formsubmitted.php" method="post"> <table width="465" border="0"> <tbody> <tr> <td width="186"><label for="type_writing">type of writing:</label></td> <td width="269"><select ng-change="calculate()" ng-model="type_writing" ng-options="o o.text o in typeofwritings" ng-init="type_writing='5'" name="type_writing" id="type_writing"> </select> </td> </tr> <tr> <td><label for="num_pieces">number of pieces:</label></td> <td><select ng-change="calculate()" ng-model="piece" ng-options="o o o in pieces" name="num_pieces" id="num_pieces"></select></td> </tr> <tr> <td> </td> <td><strong>order cost</strong> <strong>{{type_writing.text}}</strong> <strong>{{piece}}</strong> pages<div style="color:#d76d25; font-size:24px;">${{calculation}}</div></td> </tr> </tbody> </table> </form> </div> </body> <script> (function(angular) { 'use strict'; angular.module('formexample', []) .controller('examplecontroller', ['$scope', function($scope) { $scope.typeofwritings = [{ value: '5', text: 'writing scratch' }, { value: '3', text: 'editing or proofreading' } ]; $scope.type_writing = $scope.typeofwritings[0].value; $scope.pieces = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; $scope.piece = 1; $scope.calculate = function(){ $scope.calculation = ($scope.piece * $scope.type_writing.value); } $scope.calculate(); }]); })(window.angular); </script>
Comments
Post a Comment