Legaldirectmoney Legaldirectmoney - 5 months ago 11
AngularJS Question

AngularJS - 2 Inputs for one variable

I need to make two inputs, each one connected to each model, then these variables, should be concatenated and assigned to a third variable.

So:
Input A : "abc"
Input B : "dfg"
variable = "abcdfg"

Answer

Here's the simplest way I can think of. It uses the ng-change event to run an angular expression that sets the value of the combined variable.

angular.module('myApp', []);
<script data-require="angularjs@1.5.5" data-semver="1.5.5" src="https://code.angularjs.org/1.5.5/angular.js"></script>
<h1>Connected models</h1>
    <div ng-app="myApp">
      <input name="a" ng-model="model.A" ng-change="model.C = model.A + model.B" />
      <input name="b" ng-model="model.B" ng-change="model.C = model.A + model.B"/>
      
      <h3>Variables</h3>
      <pre>
        A: {{model.A}}
        B: {{model.B}}
        Combined: {{model.C}}
      </pre>
    </div>