0

I need to do something like this : When you select ex. "login", then in input text shows login from $scope.logins The same with password

JS:

  $scope.logins = [{
        "login" : "log",
        "password" : "pass"
    }]

HTML:

<select ng-model="type">
     <option value="" disabled selected>Type</option>
     <option>login</option>
     <option>password</option>
</select>

<input class="form-control" name="type" placeholder="value" ng-model="value" style="margin-bottom:5px;">

Thanks for answers in advance.

1
  • Why did you remove my answer? Commented Aug 2, 2017 at 12:41

2 Answers 2

2

First Method

You can use $scope.$watch to accomplish this

var app = angular.module('mainApp', []);
app.controller('mainController', function($scope) {
    $scope.logins = [{
        "login" : "log",
        "password" : "pass"
    }]
    $scope.$watch('type',function(val){
       $scope.value=$scope.logins[0][val];
    });
 });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="mainApp" ng-controller="mainController" class="col-xs-12">
<select ng-model="type">
     <option value="" disabled selected>Type</option>
     <option>login</option>
     <option>password</option>
</select>

<input class="form-control" name="type" placeholder="value" ng-model="value" style="margin-bottom:5px;"> 
</div>

Second Method

You can directly bind the logins object with respect to type in your input. (No calculations needed in js)

var app = angular.module('mainApp', []);
app.controller('mainController', function($scope) {
    $scope.logins = [{
        "login" : "log",
        "password" : "pass"
    }]

 });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="mainApp" ng-controller="mainController" class="col-xs-12">
<select ng-model="type">
     <option value="" disabled selected>Type</option>
     <option>login</option>
     <option>password</option>
</select>

<input class="form-control" name="type" placeholder="value" ng-model="logins[0][type]" style="margin-bottom:5px;"> 
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

Use ng-change for this:

<select ng-model="type" ng-change="onSelectBoxChange(type)">
     <option value="" disabled selected>Type</option>
     <option value="login">login</option>
     <option value="password">password</option>
</select>

<input class="form-control" name="type" placeholder="value" ng-model="value" style="margin-bottom:5px;">

In Controller

$scope.logins = [{
        "login" : "log",
        "password" : "pass"
    }];

$scope.onSelectBoxChange = function(selectedValue){
  if(selectedValue=="login"){
    $scope.value = $scope.logins[0].login;
  }else{
    $scope.value=undefined;
  }
}

1 Comment

I don't understand how this became the accepted answer, this won't even work for both cases?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.