Keeper
Keeper

Reputation: 61

How do I search by keyword in Angular JS

I have a set of data that goes like code:description (example: cmsc100: Web Programming). I have a search box that asks user to enter either the code or the description. When they click the button, the program must output the data that contains the code/description or nothing found if it does not exist.

The API is already correct. I have a problem in implementing it in angular. when I click the button it always outputs "O results found"

This is my Controller:

    //search by description
    $scope.byDescription = function(){
        $http
            .get('http://localhost:3000/api/search-by-desc')
            .then(function(response){
                $scope.subjects = response.data;
                console.log(response)
            },
            function(response){
                console.log(response)
                $scope.subjects = 'Nothing found'
            })

            $scope.desc = ""
    }

Upvotes: 3

Views: 568

Answers (1)

Pavel
Pavel

Reputation: 1288

Variable text is the parameter.

JavaScript

$scope.byDescription = function(text){
    $http
        .get('http://localhost:3000/api/search-by-desc?q=' + text)
        .then(function(response){
            $scope.subjects = response.data;
            // you can see how many subjects you receive from the API with
            console.log(response.data.length);

        })
        .catch(function(response){
             // Redirect to error page 
              $location.path("/error");
        });
}

HTML

<input type="text" ng-model="temp.pattern" name="pattern"/>
<button type="button" ng-click="byDescription(temp.pattern)">Search</button>

Node.js

router.get("/", (req, res, next) => {
      var q = req.query.q;

      //Search
}

Upvotes: 1

Related Questions