只需使用AngularJS和PHP Web服务进行搜索

时间:2014-10-12 14:25:38

标签: php angularjs web-services rest

我试图使用angularJS与php web服务进行简单的搜索。看看这是这个接口index.html: `

<div class="panel-body">
                        <p> cek jumlah processor perangkat</p>  
                            <form class="navbar-form navbar-default" role="navigation">
                            <div class="form-group">Filter Perangkat : </div>
                            <div class="form-group">
                            <input type="text" ng-model="cpu" placeholder="Filter" class="form-control"  />
                            </div>  
                     <button class="btn btn-info" ng-click="find(cpu)">Submit</button>
                     </form>
                    </div>

app.js上的服务:

obj.grafik5= function (cpu) {
    return $http.get(serviceBase + 'grafik5?cpu=' + cpu).then(function (status) {
        return status.data;
    });
};

然后是控制器app.js:

`app.controller('grafikku', function ($scope, $rootScope, $location, $routeParams, services, cpu) {
 $scope.find= function(cpu) {
        $location.path('/grafik');
       services.grafik5(cpu);
      };`

从php方面:

   private function grafik5()
        {           
            if($this->get_request_method() != "GET")
            {
                $this->response('',406);
            }

            $cpu=(string)$this->_request['cpu'];

                $sql = "Select * FROM data WHERE cpu ="$cpu";
                $r = $this->mysqli->query($sql) or die($this->mysqli->error.__LINE__);

                    $success = array("status" => "Success", "msg" => "Successfully one record deleted.");
                    $this->response($this->json($success),200);

        }

我已经在邮政局的php端进行了测试,并且它正常工作。但我知道我的错误是在角度方面。我真的卡在这里请帮助我,我是新的角度

1 个答案:

答案 0 :(得分:0)

return $http.get(serviceBase + 'grafik5?cpu=' + cpu).then(function (status) {
    return status.data;
});

$ http.get是异步的,所以最好在这里我认为你会回复一个承诺(见https://docs.angularjs.org/api/ng/service/$q

您可以先尝试获取数据:

obj.grafik5= function (cpu) {
      return $http.get(serviceBase + 'grafik5?cpu=' + cpu);
};

app.controller('grafikku', function ($scope, $rootScope, $location, $routeParams, services, cpu) {
 $scope.find= function(cpu) {
   services.grafik5(cpu).then(function(data){
   console.log(data);
   }), function (error){ 
       console.log(error);
       }
  };