Answer the question
In order to leave comments, you need to log in
How to assign a default value to a model in Angular?
For example, there is a field: It is <input type="text" value="123" ng-model="name">
necessary to make $scope.name equal to "123". I do it like this:
<input type="text" value="123" ng-model="name" ng-init="name = '123';">
Answer the question
In order to leave comments, you need to log in
The default value should be set in the models (or at least in the controller) and not in the view. It's simpler, more efficient, and the code is easier to maintain.
function Article() {
this.name = 'Default article name';
this.text = '# Some default article text\n\nSome paragraph';
}
angular
.module('app')
.service('Article', Article),
.controller('MainCtrl', function (Article) {
this.article = new Article();
}
;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question