4

I got a form for editing products, the value is showing in the inspector, however not in the form. They do show when I remove ng-model from my code.

In this line the value shows in the inspect element, but not in the form:

<input type="text" class="form-control" placeholder="Product naam" 
value="{{product.title}}" ng-model="formData.title"/>

In this line the value shows in the inspect element, and in the form:

<input type="text" class="form-control" placeholder="Product naam" 
value="{{product.title}}"/>

However I need ng-model to pass the data.

Any suggestions?

2 Answers 2

8

You don't need the value property in your input tag. The ng-model will already do the binding for you. If you define formData.title in your controller, it will reflect on the value of your input.

The same way, if the user changes the value of the input, it will reflect on the value declared in your controller.

This is the famous Two-way Data Binding of AngularJS. One awesome and dangerous feature of the framework

Take a look at this Fiddle:

http://jsfiddle.net/relferreira/kLcqwuqf/

HTML:

<div ng-app="app">

  <div ng-controller="MainController">
    <input type="text" data-ng-model="test">
  </div>

</div>

JS:

angular.module('app', []);

angular.module('app')
    .controller('MainController', mainController)

mainController.$inject = ['$scope'];
function mainController($scope){

  $scope.test = 'value of the input'

}
Sign up to request clarification or add additional context in comments.

3 Comments

@NickAudenaerde : if you have found your solution can you close this question.
@NickAudenaerde if it was useful, mark it as the answer. It is not for the points, but to show others that it is the solution for the problem.
@RenanFerreira : For example , data-ng-model = "formdata.test" then how we will achieve this ?
0

If data-ng-model="form.test" then how you will populate its value in input box. Just modifying answer of Renan so that it will help you out to understand the solution more easily.

    angular.module('app', []);

    angular.module('app')
      .controller('MainController', mainController)

     mainController.$inject = ['$scope'];

    function mainController($scope) {
      $scope.form = {};
      $scope.form.test = 'value of the input'

    }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">

  <div ng-controller="MainController">
    <input type="text" data-ng-model="form.test">
  </div>

</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.