javascript - 使用 json 值在 Angularjs 中自动完成

标签 javascript angularjs

我正在学习使用纯 AngularJS 和我从 YouTube 视频中复制的以下代码进行自动完成。

我卡在了这个特定的部分:

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;
};
});

服务

myApp.service("weatherService", ['$http', '$q', function ($http, $q) {
return {
    getCities: function () {
        return $http.get('data/data.json');
        }
   };
}]);

HTML(指令)

<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>

我收到一个错误,因为 city.toLowerCase 不是一个函数

最佳答案

试试这个:

var myApp = angular.module("myApp", []);
myApp.controller("myWeatherContrl", function($scope, weatherService) {
  $scope.title = "Weather App";
  
  $scope.obj = {};
  $scope.obj.data = [];
  weatherService.getCities().then(function(response) {
    angular.forEach(response.data, function(state) {

      angular.forEach(state, function(city) {

        if (typeof city === "string") {
          $scope.obj.data .push(city);
        }
      });
    });

    console.log("Say Hi", $scope.data);
  })

  $scope.complete = function(string) {
    var output = [];
    if (string.length >= 3) {
      angular.forEach($scope.obj.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;
  };
});
myApp.service("weatherService", ['$http', '$q', function($http, $q) {
  return {
    getCities: function() {
      return $http.get('https://api.myjson.com/bins/pufq5');
    }
  };
}]);
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<body>

  <div ng-app="myApp" ng-controller="myWeatherContrl">
    <div class="input-group">
      <input type="text" id="city" ng-keyup="complete(obj.city)" placeholder="Enter City Name" class="form-control" ng-model="obj.city" />
      <ul ng-if="obj.data.length>0" class="list-group">
        <li style="cursor:pointer" class="list-group-item" ng-click="obj.city = citydata;obj.data={};" ng-repeat="citydata in filterCity track by $index">
          {{citydata}}
        </li>
      </ul>
    </div>
</body>

关于javascript - 使用 json 值在 Angularjs 中自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45825552/

相关文章:

javascript - 使用 $parsers 的 Jasmine 单元测试 Angular Directive(指令)

javascript - 为什么我的测试失败并且 lodash 方法中的 console.log() 语句没有记录?

javascript - 如何在jsp中获取&lt;input type ="file"的文件名

Javascript 在 xhttp POST 请求中发送带有 zip 文件的元数据

javascript - Chrome 扩展,Jquery ajax 失败

javascript - 单击鼠标在 Canvas 中绘制一个圆圈

javascript - 期待一个赋值或函数,而是看到一个表达式

javascript - 正整数的 Angular 自定义指令

javascript - 如何从外部调用 Angularjs 工厂函数

angularjs - 如何使用 Liferay 配置 Angular JS?