简体   繁体   中英

Angularjs : Texbox value not showing

In agularjs controller I am creating variable

var search = [];
search.area = 'xyz';
$scope.search = search;

And in html page

 <input type="text" name="area" class="form-control" data-ng-model="search.area" placeholder="Area">

So, after loading the page default value doesn't being display as 'xyz' in textbox.

Please guide !!

There is some problem with your controller:-) Your code is working fine

<div ng-app="myApp" ng-controller="MyCtrl"><input type="text" name="area" class="form-control" data-ng-model="search.area" placeholder="Area"/>

    </div>
<script>
    var myApp = angular.module('myApp',[]);
    function MyCtrl($scope) {
    var search = [];
    search.area = 'xyz';
    $scope.search = search;
    }
</script>

Fiddle:- http://jsfiddle.net/4rwnefnL/

I think you are missing angular library Or controller is different in which you are declaring search variable.

Or Might be ng-app="myApp" (tell angular to initialize on page) is missing from your HTML.

Always do follow modular approach in angular by creating module.

angular.module('myApp', [])
.controller('mainCtrl', function($scope){
  // controller code here
})
.directive('myDir', function(){
  return {
    //directive code here
  }
})
.filter('customfilter', function(){
  return function(val){
    //filter code here
  }
})
.factory('customFactory', function(){
  return function(val){
    //filter code here
  }
})

Follow above code to design angular app.

Working Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM