Legaldirectmoney Legaldirectmoney - 1 year ago 69
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.

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

Answer Source

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=""></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"/>
        A: {{model.A}}
        B: {{model.B}}
        Combined: {{model.C}}