codedoll codedoll - 1 year ago 80
AngularJS Question

ng-model as initial input on form

I have an edit form that pushes data to a mongo db using express and angular. I am using ng-model for my data. The PUT works correctly to update the database. But I can't seem to make that found data as initial values on the input fields in my GET. I think I am binding thinks incorrectly. If that is the case, what am I doing wrong?
Thanks in advance.

My controller

app.controller('EditController', ['$scope', '$http', '$routeParams', function($scope, $http, $routeParams) {

var self = this;
method: 'GET',
url: '/users/' + $,
data: $
}).then(function(response) {
// console.log(; = $; =;
self.age =;
self.gender =;
self.img =;

this.editForm = function() {
console.log('Formdata: ', this.formdata);

method: 'PUT',
url: '/users/' + $,
data: this.formdata,
}).then(function(result) {
self.formdata = {}
} // end editForm

// end EditController

app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider){

$routeProvider.when('/', {
templateUrl: 'partials/match_partial.html'
}).when('/edit/:id', {
templateUrl: 'partials/edit_partial.html',
controller: 'EditController',
controllerAs: 'editctrl'



<a ng-href="/">
<h3 class="back">Back to Match</h3>
<h1 class="editHeader">
Edit {{}}
<form ng-submit="editctrl.editForm()">
<input type="text" ng-model="" placeholder="{{}}">
<input type="text" ng-model="" placeholder="{{}}">
<input type="text" ng-model="editctrl.formdata.age" placeholder="{{editctrl.age}}">
<input type="text" ng-model="editctrl.formdata.gender" placeholder="{{editctrl.gender}}">
<input type="text" ng-model="editctrl.formdata.img" placeholder="{{editctrl.img}}">
<input type="submit">

Answer Source

You can simply set the whole object to receive the, this way:

  method: 'GET',
  url: '/users/' + $,
  data: $
}).then(function(response) {
  // console.log(;      
  // Here 
  self.formdata =;

And it will automatically fills all inputs with the object properties.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download