Angular.js programmatically setting a form field to dirty
In your case, $scope.myForm.username.$setViewValue($scope.myForm.username.$viewValue);
does the trick - it makes both the form and the field dirty, and appends appropriate CSS classes.
Just to be honest, I found this solution in new post in the topic from the link from your question. It worked perfectly for me, so I am putting this here as a standalone answer to make it easier to be found.
EDIT:
Above solution works best for Angular version up to 1.3.3. Starting with 1.3.4 you should use newly exposed API method $setDirty()
from ngModel.NgModelController
.
Since AngularJS 1.3.4 you can use $setDirty()
on fields (source). For example, for each field with error and marked required you can do the following:
angular.forEach($scope.form.$error.required, function(field) {
field.$setDirty();
});