dongtuji0992 2018-09-21 14:58
浏览 85

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

Trying to make ajax request when searched data is not found in object.

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>

Controller:-

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;
 });

My Function :-

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

How to do this combining my controller, function and model.

  • 写回答

1条回答 默认 最新

  • duanqiao1926 2018-09-21 16:21
    关注

    ,This is where angular service comes into play. You should make a new file for both controllers and services. For the sake of simplicity though, you can just add the following code into your current file AFTER the controller.

       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.
               })
           }
       )
    

    Once that is done, you'll inject your service into your controller here:

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

    Next, lets call do the POST by hitting the service. Inside of your controller block, write the following:

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

    If you aren't using Gulp (or something similar), then you will need to add the service into your index.html(Or whatever file is your base html file) file just like you did(I assume) with your controller.

    Let me know if you have any questions!

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么