How to search by keyword in Angular JS

I have a dataset that looks like code: description (example: cmsc100: web programming). I have a search box that asks the user for either a code or a description. When they press the button, the program should output data containing the code / description or nothing found if it doesn't exist.

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

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 = ""
    }

      

+3


source to share


1 answer


A variable text

is a 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
}

      

+1


source







All Articles