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: https://code.ciphertrick.com/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.com/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.com">google</a></li>
  <li><a href="https://bbc.com">bbc</a></li>
  <li><a href="https://microsoft.com">microsoft</a></li>
</ul>  

</body>
</html>

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

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

7
  • <li ng-repeat="i in berkay | filter:searchString"> did you use this? 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 Commented Dec 28, 2016 at 12:38
  • @VenkateshKonatham code.ciphertrick.com/demo/angularajaxsearch I want to filter list of links. Commented Dec 28, 2016 at 12:43
  • so you want to implement like above. But the links will be static from html right? 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. Commented Dec 28, 2016 at 13:40

3 Answers 3

2

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

<script src="https://ajax.googleapis.com/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.com'
    }, {
      name: "bbc",
      link: 'https://bbc.com'
    }, {
      name: "microsoft",
      link: 'https://microsoft.com'
    }];
  });
</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>

Sign up to request clarification or add additional context in comments.

Comments

2

You can add javascript inside HTML itself

<script src="https://ajax.googleapis.com/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.com'
    }, {
      name: "bbc",
      link: 'https://bbc.com'
    }, {
      name: "microsoft",
      link: 'https://microsoft.com'
    }];
  });
</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>

4 Comments

exactly what i need for filtering but links are not working - I mean not redirecting to the pages
@abidinberkay I have added the comments inside the HTML for filtering with link and name, link or name. Hope this Helps!!!
thanks for filtering. But I want links to be able to work/redirect. So when I choose the filtered links it must redirect. In your code filtering is working but links are not working.
@abidinberkay Sorry I missed the {{}} braces. I have updated my answer.
1

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

<!DOCTYPE html>
<html>
<body>
<script src="https://ajax.googleapis.com/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.com"},{Text:"https://bbc.com"},{Text:"https://microsoft.com"}];
$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>   

6 Comments

I have no another file, all codes must be in a single html file. So how can I add your suggestion to my html file?
output is only {{item.value}} . there is no list on screen
still only result on page is {{item.value}}
well. the filter works great but data is text, I will add links as data so when I click on data it must redirect to corresponding site. Can I add them as link?
If it works for you, could you check the thick and resolve your question?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.