Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
26
rated 0 times [  27] [ 1]  / answers: 1 / hits: 7991  / 10 Years ago, sat, august 16, 2014, 12:00:00

I am a beginner in angular. I am able to make drop down and able to get it value using ng-model. But I need to get different value on input field. If the value of drop down value is blank then input field value is blank. When drop down value is one input filled with rahul so I will set these value while checking the value of drop down?



Plunker: http://plnkr.co/edit/wVo9SW8Lc0uVr213grR2?p=preview



<!DOCTYPE html>
<html>
<head>
<link [email protected] data-semver=3.2.0 rel=stylesheet href=//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css />
<script data-require=angular.js@* data-semver=1.3.0-beta.5 src=https://code.angularjs.org/1.3.0-beta.5/angular.js></script>
<script [email protected] data-semver=0.10.0 src=http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.10.0.js></script>
<link rel=stylesheet href=style.css />
<script src=script.js></script>
</head>

<body ng-app=app ng-controller='cntrl'>
<select ng-model=myColor ng-options=color.name for color in colors></select>
<button ng-click=getColor()>getvalue</button>
<input type =text/>
</body>

<script>
angular.module('app',[]).controller('cntrl',function($scope){
$scope.colors=[{name:one},{name:two},{name:tree}]
$scope.getColor=function(){
alert('--'+$scope.myColor.name)
}
})
</script>
</html>


Is there no way to check value of drop down? Is there any way to check onchange listener in angular? Of is there any if-else condition?


More From » jquery

 Answers
4

You can bind the value of the input field to a javascript function using <input type=text value={{getValue()}}>. The getValue() function can then check the myColor variable and return accordingly. I also simplified the model a bit, colors is now an array of strings. When myColor===one, getValue returns rahul which populates the input, else it returns ;



See: plunkr


[#43078] Friday, August 15, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
allans

Total Points: 336
Total Questions: 120
Total Answers: 119

Location: Peru
Member since Mon, Jun 6, 2022
2 Years ago
;