I'm trying to pass some data to a angularjs form with no success.
I'm defining a module, calling it in the template and binding the template to a controller. The idea is to pass something to the $scope variable in order to show the existing username as value to the field. The same form is going to be used to add and edit users. In the last case, I'll fetch the user by id and pass his info to the $scope
through the myForm
variable.
Here's a plunker to make things easier: http://plnkr.co/edit/nAabxgrw4HqLcgrErVxq?p=preview
Template:
<div ng-app="App">
<form ng-controller="TestController" class="form-horizontal" name="myForm" novalidate>
<div class="form-group" ng-class="{ 'has-error' : myForm.username.$invalid && !form.username.$pristine }">
<div class="col-md-6">
<label class="control-label no-padding-right" for="username">Nome</label>
<input
name="username"
id="username"
ng-model="username"
type="text"
class="form-control"
ng-required="true" />
<p ng-show="myForm.username.$invalid && !myForm.username.$pristine"
class="help-inline no-padding">This field is required</p>
</div>
</div>
<p>{{ myForm.username }}</p>
</form>
</div>
Code:
window.angular.module('App', []);
window.angular.module('App').controller('TestController', ['$scope', function ($scope) {
$scope.myForm = {};
$scope.myForm.username = 'John Doe';
}]);
Any help would be awesome.
You input is bind to username
ng-model="username"
so in your controller you can set up username by :
Please see demo below