繁体   English   中英

如果在angular.js的对象中找不到,如何在数据库中搜索项目?

[英]How to search item in database if not found in object in angular.js?

在对象中找不到搜索到的数据时尝试发出ajax请求。

HTML: -

Search programming languages: <input type="Text" ng-model="out.pl">
<div ng-controller="MyContRollEr">
   <table border="2">
       <tr>
           <td>Programming Language:</td>
           <td>Percentage:</td>
           <td>Salary:</td>
           <td>People:</td>
           <td>Started Date:</td>
       </tr>
     <tr ng-repeat="data in info | filter:out">
       <td>{{data.pl}}</td>
       <td>{{data.percent}}</td>
       <td>{{data.salary |currency:'Rs.'}}</td>
       <td>{{data.people | number :2}}</td>
       <td>{{data.date | date:"yyyy/MM/dd"}}</td>
    </tr>
  </table>

控制器: -

var app = angular.module('app',[]);
app.controller('MyContRollEr',function($scope) {
  var info = [
       {pl:'php',percent:'10%',salary:10000000,people:234524},
       {pl:'Java',percent:'20%',salary:9822200,people:234443},
       {pl:'python',percent:'10%',salary:8739300000,people:2345521)},
     ];
     $scope.info = info;
 });

我的功能:-

  function sendRequest(){
     $http({
       method:'POST',
       url:'index.php',
       data:{search:'data'}
      }).then(function(data) {
        $scope.out = data;
      })
  }

如何结合我的控制器,功能和模型。

,这是角服务发挥作用的地方。 您应该为控制器和服务都创建一个新文件。 为了简单起见,您可以在控制器之后将以下代码添加到当前文件中。

   app.service('myService',function($http) {
       this.sendRequest = function() {
           $http({
               method:'POST',
               url:'index.php',
               data:{search:'data'}
           }).then(function (response) {
               console.log(response);
               return response.data; // Most APIs have the "data" in the response object. But you can take this out if the console log doesn't show that key in the object.
           })
       }
   )

完成后,您将在此处将服务注入到控制器中:

    app.controller('MyContRollEr',function($scope, myService) { // Notice that the service is a parameter in controller now.

接下来,让我们通过点击服务调用POST。 在您的控制器块内,编写以下代码:

     myService.sendRequest().then(function (response) {
         console.log(response);
     })

如果您未使用Gulp(或类似工具),则需要将该服务添加到index.html(或任何基本html文件)文件中,就像您对控制器所做的操作(我假设)一样。

如果您有任何疑问,请告诉我!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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