最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS dynamic search box on same page - Stack Overflow

programmeradmin0浏览0评论

I am new in AngularJS but I have basic HTML and JavaScript knowledge.

I have a list of links on page, and a search box. I want to filter list elements dynamically according to the text typed in the box. I have found some example but they use external lists- not a list typed on same html file.

Exactly this: /

As you guess I cannot make filtered elements visible/unvisible like this. How can I filter them (all html codes must be in 1 page so we cannot call another js file, controller or etc.)

<!DOCTYPE html>
<html>
<body>
<script src=".0.7/angular.min.js"></script>

<div class="bar">
    <input type="text" class="search" ng-model="searchString" />
</div>

<ul class="data-ctrl">
                <li ng-repeat="i in berkay | searchFor:searchString">

                </li>
</ul>

<ul name="berkay">
  <li><a href="">google</a></li>
  <li><a href="">bbc</a></li>
  <li><a href="">microsoft</a></li>
</ul>  

</body>
</html>

Note: If there is a way with just Javascript or etc. without using angular js, it is wele as well.

Last Edit: All 3 answers are correct and working fine right now.

I am new in AngularJS but I have basic HTML and JavaScript knowledge.

I have a list of links on page, and a search box. I want to filter list elements dynamically according to the text typed in the box. I have found some example but they use external lists- not a list typed on same html file.

Exactly this: https://code.ciphertrick./demo/angularajaxsearch/

As you guess I cannot make filtered elements visible/unvisible like this. How can I filter them (all html codes must be in 1 page so we cannot call another js file, controller or etc.)

<!DOCTYPE html>
<html>
<body>
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.0.7/angular.min.js"></script>

<div class="bar">
    <input type="text" class="search" ng-model="searchString" />
</div>

<ul class="data-ctrl">
                <li ng-repeat="i in berkay | searchFor:searchString">

                </li>
</ul>

<ul name="berkay">
  <li><a href="https://google.">google</a></li>
  <li><a href="https://bbc.">bbc</a></li>
  <li><a href="https://microsoft.">microsoft</a></li>
</ul>  

</body>
</html>

Note: If there is a way with just Javascript or etc. without using angular js, it is wele as well.

Last Edit: All 3 answers are correct and working fine right now.

Share Improve this question edited Dec 28, 2016 at 15:06 abidinberkay asked Dec 28, 2016 at 12:30 abidinberkayabidinberkay 2,0355 gold badges49 silver badges78 bronze badges 7
  • <li ng-repeat="i in berkay | filter:searchString"> did you use this? – Sai Commented Dec 28, 2016 at 12:35
  • can you please explain what you want to filter in the above html file. I didn't get exactly what you want to filter – Venkatesh Konatham Commented Dec 28, 2016 at 12:38
  • @VenkateshKonatham code.ciphertrick./demo/angularajaxsearch I want to filter list of links. – abidinberkay Commented Dec 28, 2016 at 12:43
  • so you want to implement like above. But the links will be static from html right? – Venkatesh Konatham Commented Dec 28, 2016 at 12:59
  • @VenkateshKonatham I did not understand what is static link. But I want the links are able to redirect. Just below, in answer of "Nagaveer Gowda" the code is working great. If you just copy it a simple notepad and open in browser you will see it is working for filtering but links are not redirecting.. The problem is this. – abidinberkay Commented Dec 28, 2016 at 13:40
 |  Show 2 more ments

3 Answers 3

Reset to default 2

Please check this updated answer. It will redirect when you click on it.

<script src="https://ajax.googleapis./ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script>
  var app = angular.module("app", []);
  app.controller("myCtrl", function($scope) {
    $scope.berkay = [{
      name: "google",
      link: 'https://google.'
    }, {
      name: "bbc",
      link: 'https://bbc.'
    }, {
      name: "microsoft",
      link: 'https://microsoft.'
    }];
  });
</script>
<div ng-app="app" ng-controller="myCtrl">
  <div class="bar">
    <!--
    1. If you want to search everything(both link and name), just use searchString 

      <input type="text" class="search" ng-model="searchString">

    2. If you want to search only name change searchString to searchString.name

      <input type="text" class="search" ng-model="searchString.name">

    3. If you want to search only link change searchString to searchString.link

      <input type="text" class="search" ng-model="searchString.link">
    -->
    <input type="text" class="search" ng-model="searchString" />
  </div>

  <ul class="data-ctrl">
    <li ng-repeat="i in berkay | filter:searchString">
      <a href="{{i.link}}">{{i.name}}</a>
    </li>
  </ul>
</div>

You can add javascript inside HTML itself

<script src="https://ajax.googleapis./ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script>
  var app = angular.module("app", []);
  app.controller("myCtrl", function($scope) {
    $scope.berkay = [{
      name: "google",
      link: 'https://google.'
    }, {
      name: "bbc",
      link: 'https://bbc.'
    }, {
      name: "microsoft",
      link: 'https://microsoft.'
    }];
  });
</script>
<div ng-app="app" ng-controller="myCtrl">
  <div class="bar">
    <!--
    1. If you want to search everything(both link and name), just use searchString 

      <input type="text" class="search" ng-model="searchString">

    2. If you want to search only name change searchString to searchString.name

      <input type="text" class="search" ng-model="searchString.name">

    3. If you want to search only link change searchString to searchString.link

      <input type="text" class="search" ng-model="searchString.link">
    -->
    <input type="text" class="search" ng-model="searchString" />
  </div>

  <ul class="data-ctrl">
    <li ng-repeat="i in berkay | filter:searchString">
      <a href="{{i.link}}">{{i.name}}</a>
    </li>
  </ul>
</div>

Could you try this. I hope it works for you :)

<!DOCTYPE html>
<html>
<body>
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.0.7/angular.min.js"></script>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $filter) {
$scope.searchString=[{Text:"https://google."},{Text:"https://bbc."},{Text:"https://microsoft."}];
$scope.searchString2=$scope.searchString;
$scope.$watch('search', function(val)
    { 
        $scope.searchString= $filter('filter')($scope.searchString2, val);
    });
});
</script>

<div ng-app="myApp" ng-controller="myCtrl">
<div class="bar">
    <input type="text" class="search" ng-model="search" />
</div>


 <ul>
    <li ng-repeat="item in searchString">
    <a href="{{item.Text}}">{{item.Text}}</>

    </li>
    </ul>
</div>
</body>
</html>   
发布评论

评论列表(0)

  1. 暂无评论