Mohammed Nasrullah
Mohammed Nasrullah

Reputation: 433

Angular, get dynamic value of textbox to controller

i fetch from json and set the value to my angular ionic app. my textbox holds the value. but im unable to get the textbox value to controller. this is how i have done it

controller.js

$http.post("http://www.fooget.com/mydetails.php).success(function(details){
$scope.mydetails= details;
});

and i set the value to my html page

<form>
<div  ng-repeat="data in details|limitTo:1">
<p>{{data.f_name}}</p> <!--displays the value-->
<input type="text" ng-model="v.name" value="{{data.f_name}}"/> <!--empty values-->
<input type="text" ng-model="v.id" value="{{data.id}}"/> <!--empty values-->
<button ng-click="push(v)">
</form>

on form click i dont get the textbox values to my controller, im trying to get the vaules to the controller. it doesnt appear

$scope.push= function (v) { 
var push_name = v.name; // empty values
var push_id = v.id; // empty values
}

Upvotes: 0

Views: 2412

Answers (1)

mohamedrias
mohamedrias

Reputation: 18566

Also in your HTML:

<div  ng-repeat="data in details|limitTo:1">

It should be

<div  ng-repeat="data in mydetails|limitTo:1">

as you have $scope.mydetails and not details

The real problem is in initializing the values for ng-model:

As you're just setting the value attribute in input text, it's not assigned to the ng-model. Use ng-init directive to set it in the view.

<input type="text" ng-model="v.name" ng-init="v.name=data.f_name"/> 
<input type="text" ng-model="v.id"  ng-init="v.name=data.id"/>

This will work for you.

Upvotes: 1

Related Questions