Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: How to set default value to ng-model="searchText"?

Tags:

html

angularjs

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?

like image 323
Sachin B. R. Avatar asked May 27 '15 11:05

Sachin B. R.


People also ask

How do I set default value in Ng?

Use ng-init to set default value for ng-options .

How do you initialize NgModel?

In the code, there is a input which id is 'myInput1' has bundled a ngModel which name is 'myModel1'. The value of the input we give it 'A'. And we put this input tag into the ModelController1 angular controller.

What is the difference between ng-model and data NG model?

For AngularJS there is no difference between ng-app and data-ng-app or ng-controller and data-ng-controller or ng-model and data-ng-model because while compiling HTML page, AngularJS strips data- or x- prefix. Find the URL for reference.

Can we use NG-model for Div?

NgModel expects the bound element to have a value property, which div s don't have. That's why you get the No value accessor error. I don't know if the input event is supported on all browsers for contenteditable . You could always bind to some keyboard event instead.


3 Answers

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 insteat of value

<input ng-model="searchText" ng-init="searchText ='can you see me'"></input>
like image 155
Marwen Cherif Avatar answered Nov 03 '22 19:11

Marwen Cherif


<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

like image 31
Ashish Mehta Avatar answered Nov 03 '22 18:11

Ashish Mehta


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>
like image 34
carton Avatar answered Nov 03 '22 20:11

carton