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?
Use ng-init to set default value for ng-options .
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.
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.
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.
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>
<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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With