user4520762
user4520762

Reputation:

How use ng-value and ng-model in form inputs text

I'm making a form to edit information in a database, this form is working, he's taking the information depending on what I select, however it is not showing in text inputs on the page.

I know the information that i'm getting is the right information because I can see on the console and also in the attribute 'value', although not appear on the page.

This page have two inputs text:

  <label for="meetingPoint">Name:</label>
  <input type="text" class="form-control" name="name" rows="5" ng-value="{{ data[0].name }}" ng-model="formData.name" />

  <label for="meetingPoint">Meeting Point:</label>
  <input type="text" class="form-control" name="meetingPoint" rows="5" ng-value="{{ data[0].meetingPoint }}" ng-model="formData.meetingPoint" />

I want be able to see the information on the page and still send to my formData scope, but i'm very lost, is the first time that i'm using the Angular.

This is my controller:

var event = $routeParams.evento;
$scope.data;
$scope.submitForm;
$scope.formData = {};

$http.get('/api/data/event/edit/' + event)
  .success(function (data, status, headers, config) {
    $scope.data = data;
    console.log(data);
  })
  .error(function (data, status, headers, config) {
    $scope.data = data;
  });

$scope.processForm = function () {
  console.log($scope.formData._id);
  $http({
      method: 'PUT',
      url: '/api/data/event/edit' + $scope.formData._id,
      data: $scope.formData,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    })
    .success(function (data) {
      console.log(data);

      if (!data.success) {
        $scope.errorName = data.errors.name;
      } else {
        $scope.message = data.message;
      }
    });
};

So i'm getting the event that i want, them i'm trying to show the information with ng-value, but isn't working, and also edit my formData object.

Can someone give me a hand here? :)

Thanks!!!!

Upvotes: 1

Views: 5084

Answers (1)

mohamedrias
mohamedrias

Reputation: 18566

ng-value="{{ data[0].name }}"

Angular DOCS:: ng-value is not appropriate for input text

Binds the given expression to the value of or input[radio], so that when the element is selected, the ngModel of that element is set to the bound value.

You must use ng-model for input text box.

Upvotes: 1

Related Questions