简体   繁体   中英

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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM