Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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 data-require="[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 data-require="[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?

share|improve this question
up vote 0 down vote accepted

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

share|improve this answer
    
great answer ...!!! thanks but there is problem in your it call two time plnkr.co/edit/lgBcLP2yEGTk172QnPi8?p=info – Pallavi Sharma Aug 17 '14 at 1:30
    
when you change select option it show two time alert – Pallavi Sharma Aug 17 '14 at 1:36
    
If you need it to be called once, you can use $scope.$watch to watch the variable and update the variable bound to the input accordingly. See plunker – Josh Aug 17 '14 at 18:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.