ihemant360 ihemant360 - 6 months ago 14
AngularJS Question

Angularjs : i want to insert value to input field on click

<input type='text' ng-model="mydata" />
<span ng-bind="$parent.$eval(mydata)"></span> <br>

<button ng-repeat="button in buttons" ng-click =" mydata = button">
{{button}}</button>


Here i have array of buttons and when i click one button it will be inserted in input field without removing previous value.

Answer

Problem is that ng-repeat creates new scope. You need to do ng-click="$parent.mydata=button"

check it out here: http://plnkr.co/edit/yrIR6L0tDBI1IaCkaKYw?p=preview

<div ng-controller="Ctrl">
    <input type='text' ng-model="mydata"  />
    <button ng-repeat="button in buttons" ng-click="$parent.mydata = $parent.mydata.toString() + button">{{button}}</button>
</div>