如何在Angular JS

时间:2017-05-01 04:26:23

标签: javascript angularjs

我有一组类似代码的数据:description(例如:cmsc100:Web Programming)。我有一个搜索框,要求用户输入代码或说明。当他们单击按钮时,程序必须输出包含代码/描述的数据,或者如果不存在则找不到任何内容。

API已经正确无误。我在角度实现它时遇到了问题。当我点击按钮时,它总是输出“O results found”

这是我的控制器:

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

1 个答案:

答案 0 :(得分:1)

变量text是参数。

的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
}