AngularJS select with hard coded options -


q. why drop down not showing selcted value ? here javascript code have

<div ng-app="myapp">  <div ng-controller="myctrl">      <select ng-model="mymodel.active" convert-to-number>        <option value="-1">select</option>        <option value="1">yes</option>        <option value="2">no</option>        <option value="3">maybe</option>      </select>      <span>{{mymodel.active}}</span>  </div> </div> 

the javascript

var myapp = angular.module("myapp",[]); myapp.controller("myctrl", function($scope){       $scope.mymodel = {};       $scope.mymodel.active=2; //   }); 

the js-fiddle link -- https://jsfiddle.net/shatherali/4mxwzl5y/19/

edit: please note dont want use string value. active has number value.

this solves issue:

var myapp = angular.module('myapp', []);  myapp.controller('myctrl', function ($scope) {       $scope.mymodel = {};       $scope.mymodel.active = '2'; // it's string }); 

demo


the convert-to-number directive works fine.

var myapp = angular.module('myapp', []);  myapp.directive('converttonumber', function () {     return {         require: 'ngmodel',         link: function(scope, element, attrs, ngmodel) {             ngmodel.$parsers.push(function(val) {                 return parseint(val, 10);             });             ngmodel.$formatters.push(function(val) {                 return '' + val;             });         }     }; }); 

demo


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -