2015-07-10 2 views
0

Я пытаюсь объединить 3 ввода входного текста и привязать данные к другому входному тексту.Как связать текст с текстового ввода с другим текстовым вводом с помощью angularjs

<input type="text" class="textbox" name="country" id="loc_country" value="Gershon" ng-model="location.country"/> 
<input type="text" class="textbox" name="city" id="loc_city" ng-model="location.city"/> 
<input type="text" class="textbox" name="street" id="loc_street" autocomplete="off" ng-model="location.street"/> 
<input type="text" class="textbox" name="result" id="result"/> 

Здесь первые 3 входа должны добавлены к 4 входа автоматически с обязательным

+0

почему третий из них является поле ввода? Вам действительно нужна двойная привязка? – Rebornix

+0

Вы пробовали эту ng-model = "location.address = location.country + '' + location.city + '' + location.street '' на четвертом входе? –

+0

Rebornix, четвертый вход внутри входа карты Google Auto завершен, поэтому для вашего вопроса, да :). – user1684140

ответ

0

Вы должны установить атрибут value HTML текстового поля. Вы можете сделать это так:

<input type="text" class="textbox" name="result" id="result" value="{{location.street + ' ' + location.city + ' ' + location.country}}"/> 

Или вы можете использовать вычисленное свойство. Вот пример:

HTML:

<div ng-app> 
    <div ng-controller="SomeCtrl"> 
     <input ng-model="textProperty"/> 
     <input value="{{getCalculatedText()}}"/>    
    </div> 
</div> 

JS:

function SomeCtrl($scope) { 
    $scope.textProperty = "some text"; 
    $scope.getCalculatedText = function() { 
    return $scope.textProperty+ " is now calculated";}; 
    } 
0

Пожалуйста, смотрите мой пример ответа здесь: https://jsfiddle.net/em0ney/bc3chrog/2/

function ExampleController($scope) { 
    $scope.location = {country: 'Australia', city: 'Sydney', street: 'Pitt St'}; 
    $scope.concatLocationComponents = function() { 
     return $scope.location.street + ' ' + $scope.location.city + ', ' + $scope.location.country; 
    }; 
} 

Удачи форматирования результата.

Спасибо, Elliott