angular实现输入框输入添加 搜索框查询

!DOCTYPE html>
<html lang="en">
<head>  <meta charset="UTF-8">  <title>Title</title>  <style>  body{  position: relative;  }  ul{  width: 400px;  height: 300px;  border: 1px solid #000;  }  li{  list-style: none;  }  .pop{  width: 300px;  height: 200px;  border: 1px solid #000;  background: #eee;  text-align: center;  position: absolute;  top: 50%;  left: 50%;  margin-left: -150px;  margin-top: -100px;  }  </style>  <script src="../js/lib/angular.min.js"></script>  <script>  var myapp=angular.module("myapp",[]);  myapp.controller("myCtrl",function($scope){  $scope.data=["早上花了5元早饭", "中午花了20元午饭","aa"];  $scope.show=false;  $scope.title="";  $scope.btn="";  $scope.add="";  $scope.search="";  //添加内容  $scope.addFun=function(){  var hasLi=false;  if($scope.add.length==0){  alert("输入内容不能为空");  }else{  for(var i=0;i<$scope.data.length;i++){  if($scope.data[i]==$scope.add){  hasLi=true;  break;  }else{  hasLi=false;  }  }  }  if(hasLi==true){  $scope.show=true;  $scope.title="存在";  $scope.btn="好吧";  }else if($scope.add.indexOf("#")!=-1){  $scope.show=true;  $scope.title="输入了敏感字";  $scope.btn="很好吗?";  }else{  $scope.data.unshift($scope.add);  $scope.add="";  }  };  //点击好吧删除弹框  $scope.hide=function(){  $scope.show=false;  };  //查找内容  $scope.searchFun=function(){  var sea=false;  for(var i=0;i<$scope.data.length;i++){  if($scope.data[i]==$scope.search){  sea=true;  break;  }else{  sea=false;  }  }  if(sea==true){  $scope.show=true;  $scope.title="搜到";  $scope.btn="很好";  }else{  $scope.show=true;  $scope.title="没搜到";  $scope.btn="失望";  }  }   })  </script> </head> <body ng-app="myapp" ng-controller="myCtrl"> <h2>记账本</h2> <ul>  <li ng-repeat="item in data track by $index">{{item}}</li> </ul> <div>  <span>输入框</span><input type="text" ng-model="add"><br/>  <button ng-click="addFun()">记录</button> </div> <div>  <span>搜索框</span><input type="text" ng-model="search"><br/>  <button ng-click="searchFun()">搜索</button> </div> <div class="pop" ng-show="show">  <p>提示</p>  <p>{{title}}</p>  <button ng-click="hide()">{{btn}}</button> </div> </body> </html>

转载于:https://www.cnblogs.com/yangzhewoaini/p/7676702.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 安装必要的依赖 使用 Angular 框架实现高德地图需要安装 @types/amap-js-api 和 @types/amap-js-sdk 两个依赖。 ```bash npm install @types/amap-js-api @types/amap-js-sdk --save-dev ``` 2. 配置 highCharts API Key 在 app.module.ts 中配置高德地图的 API Key: ```typescript import { AMapLoaderService, LoaderOptions } from '@delon/theme'; // ... const options: LoaderOptions = { key: '你的高德地图 API Key', }; // ... @NgModule({ imports: [ // ... ], declarations: [ // ... ], providers: [ // ... AMapLoaderService, { provide: 'DA_STORE_TOKEN', useValue: {}, }, { provide: 'HIGHCHARTS_MODULES', useFactory: () => [more, exporting], }, { provide: HIGHCHARTS_MODULES, useFactory: () => [highcharts], } ], }) export class AppModule {} ``` 3. 创建地图的 wrapper 服务类 我们需要创建一个 wrapper 类,封装高德地图的调用,以便在内部进行维护和扩展。 ```typescript import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class MapService { private AMap: any; constructor() { this.loadMapScript(); } private loadMapScript() { const script = document.createElement('script'); script.type = 'text/javascript'; script.async = true; script.src = `https://webapi.amap.com/maps?v=1.4.15&key=${environment.amapApiKey}&callback=initAMap`; script.onerror = () => { console.error('Could not load map script!'); }; document.body.appendChild(script); } public getMapInstance() { return new Promise<any>((resolve, reject) => { if (this.AMap) { resolve(this.AMap); } else { const callbackName = `initAMap${Math.floor(Math.random() * 10000)}`; window[callbackName] = () => { this.AMap = window.AMap; resolve(this.AMap); delete window[callbackName]; }; const script = document.createElement('script'); script.type = 'text/javascript'; script.async = true; script.src = `https://webapi.amap.com/maps?v=1.4.15&key=${environment.amapApiKey}&callback=${callbackName}`; script.onerror = () => { console.error('Could not load map script!'); delete window[callbackName]; reject(); }; document.body.appendChild(script); } }); } } ``` 在上面的代码中,我们加载了高德地图 SDK 并在异步加载结束后返回 AMap 实例。 4. 在组件中使用地图查询服务 现在我们可以在组件中使用地图查询服务了。让我们创建一个 search.component.ts 文件实现这个服务。 ```typescript import { Component, OnInit } from '@angular/core'; import { MapService } from '../services/map.service'; @Component({ selector: 'app-search', templateUrl: './search.component.html', styleUrls: ['./search.component.scss'] }) export class SearchComponent implements OnInit { constructor(private mapService: MapService) {} ngOnInit() {} searchPlace(query: string) { this.mapService.getMapInstance().then(AMap => { const placeSearch = new AMap.PlaceSearch({ pageSize: 10, pageIndex: 1, city: '全国', map: new AMap.Map('map', { zoom: 15, resizeEnable: true, }), panel: 'panel' }); placeSearch.search(query, function (status: any, result: any) { // 处理查找结果 }); }); } } ``` 这里我们定义了一个 `searchPlace` 方法,用来查询地点。当我们调用 `searchPlace(query)` 的时候,它将使用 `MapService` 实例来获取 AMap 实例,然后使用 AMap 的 `PlaceSearch` 类查询地点。 5. 在 HTML 模板中添加搜索输入框和地图 最后,我们将在 HTML 模板中创建搜索输入框和地图。 ```html <div class="search-container"> <input type="text" [(ngModel)]="searchQuery" placeholder="请输入地点名称"> <button (click)="searchPlace(searchQuery)">搜索</button> </div> <div id="map" style="height: 400px;"></div> <div id="panel"></div> ``` 现在我们已经实现了一个简单的搜索地点页面,它可以通过高德地图查询地点并在地图上展示出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值