If you want to achieve two-way binding, make sure to utilize the ng-model
directive. The ng-bind
directive only facilitates one-way data synchronization, updating the element with the model value during a digest cycle. It is essential to employ the directive definition for form controls like input
, which relies on the optional ng-model
directive for functionality.
The element directives, including events such as change/input
, are registered based on the presence of the optional ng-model controller on the target element. This controller handles the view-value, model-value, and triggers the digest cycle upon specific events. Additionally, recent versions of Angular offer ng-model-options
for specifying the timing of model updates and form validation at either the element or global level.
To implement this approach, consider the following:
<input type="text" ng-model="user" name="user"></input>
<p>{{user}}</p>
While your current setup functions correctly, it's recommended to include ng-controller="cont"
usage to prevent all properties from being attached to the rootScope. A revised structure might look like:
<body ng-app="myApp">
<div ng-controller="cont">
<input type="text" ng-model="user" name="user"></input>
<p>{{user}}</p>
</div>
</body>