Initialize Values Within the Controller
The recommended approach is to set initial values in your controller like this:
angular.module('MyApp').controller(
'myController',
function ($scope, DataService) {
$scope.defaultModel = { name: '' };
// If using a data service
DataService.getModel().then(function (data) {
$scope.defaultModel = data;
// Check if model has a specific property and assign default value if not defined
if (angular.isUndefined($scope.defaultModel.name)) {
$scope.defaultModel.name = '';
}
});
);
For more information on initializing $scope variables within a controller, you can refer to this resource.
An Alternative Approach: ng-init Directive
The ng-init directive in AngularJS documentation typically serves for aliasing special properties, particularly within ng-repeat. However, in your case, it's advisable to handle initialization in the controller.
If needed, you can resort to ng-init:
<input type="text"
name="model"
ng-model="defaultModel.name"
ng-init="defaultModel.name=''">
</input>