M
M
Mikhail Shatilov2015-01-05 00:02:15
Angular
Mikhail Shatilov, 2015-01-05 00:02:15

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';">

But in the case of textarea, when there can be a lot of text, it slows down the browser a lot and in general, it looks like a crutch.
How to assign value in another way?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-01-05
@iproger

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();
    }
;

I
Ilya, 2015-01-05
@FireGM

Write inside the controller
If this is aria text, then just inside the {{ name }} tags

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question