I am learning to do autocomplete using pure AngularJS and the below code which I have copied from YouTube video.
I am stuck on this particular part:
Controller
myApp.controller("myWeatherContrl", function ($scope, weatherService) {
$scope.title = "Weather App";
weatherService.getCities().then(function (response) {
$scope.data = response;
//console.log("Say Hi", $scope.data);
})
$scope.complete = function (string) {
var output = [];
angular.forEach($scope.data, function (city) {
if (city.toLowerCase().indexOf(string.toLowerCase()) >= 0) {
output.push(city);
}
});
$scope.filterCity = output;
};
$scope.fillTextbox = function(string){
$scope.city = string;
$scope.hidethis = true;
};
});
Service
myApp.service("weatherService", ['$http', '$q', function ($http, $q) {
return {
getCities: function () {
return $http.get('data/data.json');
}
};
}]);
HTML (Directive)
<div class="input-group">
<input type="text" id="city" ng-keyup="complete(city)" placeholder="Enter City Name" class="form-control" ng-model="city"/>
<ul class="list-group">
<li class="list-group-item" ng-model="hidethis" ng-hide="hidethis" ng-click="fillTextbox(citydata)" ng-repeat="citydata in filterCity">
{{citydata}}
</li>
</ul>
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Weather App</title>
<link href="Content/bootstrap.min.css" rel="stylesheet" />
<link href="style/style.css" rel="stylesheet" />
</head>
<body ng-app="myWeatherApp" ng-controller="myWeatherContrl">
<div class="container">
<label ng-cloak>{{title}}</label>
<ui-view></ui-view>
</div>
<script src="Scripts/angular.min.js"></script>
<script src="Scripts/angular-ui-router.min.js"></script>
<script src="app.js"></script>
<script src="Scripts/controller/weathercntrl.js"></script>
<script src="Scripts/service/weather.js"></script>
</body>
</html>
I am getting an error as city.toLowerCase is not a function.
$scope.datais an array of strings, astoLowerCase()is a prototype ofString. You are getting the error "city.toLowerCase is not a function" because at the point of executing theangular.forEachmethod, thetypeofcityin your loop is not "string".