angularjsangularjs-1.6

How to show value in input field with ng-model?


I created edit form so I have to include default value for each text field. I wrote this HTML code:

<input type="text" ng-model="projectData.title" ng-init="projectData.title=title" name="title" class="form-control" />

In controller:

$scope.title = "This is just title";

It shows nothing in the text box. I tried ng-init="projectData.title={{title}}" and ng-init="projectData.title='title'" but it's just not working.

I'm using angularjs 1.6 and the following solution is not working too.

http://jsfiddle.net/Aejvm/337/


Solution

  • In your scope, you should declare the object like so:

    $scope.projectData = {};
    $scope.projectData.title = "This is just title";