2015-07-10 86 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个输入与结合

+0

为什么第三个是输入框?你真的需要双向绑定吗? – Rebornix

+0

第四个输入是否尝试了这个ng-model =“location.address = location.country +''+ location.city +''+ location.street'' –

+0

Rebornix,第四个输入是google map输入auto complete,所以对于你的问题,是的:)。 – user1684140

回答

0

你应该设置文本框的value HTML属性添加到4个输入自动。你可以这样做内联:

<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

相关问题