19

I am unable to set a default value to ng-model="searchText". Here is the code I am using
<input ng-model="searchText" value="can you see me" />

Can anyone help me with this?

2
  • 1
    ng-init:searchText="Some Text"..But i suggest you to initialize this in controller Commented May 27, 2015 at 11:41
  • Bhanu try to avoid ng-init. you should initiliaze data in controller Commented May 27, 2015 at 11:46

6 Answers 6

41

1st Solution: is to simply init the searchText in the controller.

App.controller('mainController',['$scope', function($scope) {
   $scope.searchText  = "can you see me";
}]);

2nd Solution: is to use ng-init instead of value

<input ng-model="searchText" ng-init="searchText ='can you see me'"></input>
Sign up to request clarification or add additional context in comments.

1 Comment

2nd solution sets the model value in the DOM, but in controller i yet get undefined when another element is calling ng-init..
10

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="">
<div >
  <input ng-model="searchText" type="text" ng-init="searchText='your text'"/>
</div>  
  </div>  

you can use ng-init

1 Comment

nice answer Bhudev :)
5

Try this following code:

     <input ng-model="searchText" ></input>

In your controller

    $scope.searchText = "your default value";

Edit : If you don't want to initialize the default value in controller just do

      <input value="your default value" ></input>

2 Comments

You can do it in 2 ways..One is Using a controller and the other is directly assigining it..The above approach is the best approach to do it
If you do, <input value="default value" ng-model="text" />, won't the value override the text set in ng-model?
4

Do not use ng-init on your input field to set the value. That's not required.

Just set the scope of your model to the value you would like. If it's simply placeholder text, it would be better to use the html5 placeholder attribute on your input element.

<input type="text" ng-model="someText" placeholder="Initial Text">

This will give you some text in the input field without the need to add anything to the controller scope.

If you want the text as a value you can pass without filling in the input field than you would need to set that in your controller.

$scope.someText = "Some Text";

Comments

1

You can set default value in the controller. Like this:

app.controller('myCtrl', function($scope){
   $scope.searchText = "Some default value";
});

And keep value attribute blank on input tag.

1 Comment

thank you so much it is so easy but i am beginer in angularjs so i found solution from u thanks
0

For later angular versions:

In your ts file:

export class SearchComponent implements OnInit {
  search: string;


  ngOnInit(): void {
    this.search = "Some default value";
  }
}

html file contains:

<input [(ngModel)]="searchText">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.