2017-04-11 64 views
0

我需要通過提供輸入日期來檢索服務中的輸出數據。數據在服務上執行,但我無法將輸出數據返回給控制器。我做了一個代碼的鏈接。鏈接是 Plaunker鏈接在Angular中返回服務控制器的數據

+0

https://plnkr.co/edit/4Uho7njMAXryTDOfwcLP?p=preview –

+0

着增加與問題 –

回答

3

您需要return$http.get和內部.then最終修改變量,說obj

像這樣:

return $http({ 
    url: 'miqaat.json', 
    method: 'GET', 
}) 
.then(function(response) { 

    var all_miqaats = response.data.response; 
    var obj = all_miqaats.find(function(v) { 
     return v.miqaat_date == dat 
    }); 

    if (obj == undefined || obj == "") { 
     console.log("obj = "); 
     console.log(obj); 
     obj.isHoliday = "100"; 
     // return obj; 
    } else { 
     obj.mq_title = obj.title; 
     obj.isHoliday = "101"; 
     // return obj; 
     console.log("Data from Service"); 
     console.log(obj); 
    } 

    return obj; 
}) 

而且,因爲它返回的承諾,在控制器,你需要將其更改爲:

getMiqaat.findMiqaat($scope.date).then(function(res) { 
    $scope.miqaat = res; 
}); 

無論你應該將你的.thencatchcontroller或不應該取決於你有多少代碼reuse。所以,直到我知道細節之後纔會對此發表評論。

working demo

+0

感謝Tanmay ..這是我的工作.. –

+0

@ R.Sharma高興我可以幫忙! :) – tanmay

1

而不是從工廠捕捉承諾。從控制器捕捉它。只在工廠內返回http。

app.controller('myCtrl', function($scope, getMiqaat) { 
    $scope.date = "2017-04-11"; 
    getMiqaat.findMiqaat($scope.date) 
     .then(function(response) { 
      var all_miqaats = response.data.response; 
      var obj = all_miqaats.find(function(v) { 
       return v.miqaat_date == $scope.date 
      }); 
      if (obj == undefined || obj == "") { 
       console.log("obj = "); 
       console.log(obj); 
       obj.isHoliday = "100"; 
       $scope.miqaat = obj; 
      } else { 
       obj.mq_title = obj.title; 
       obj.isHoliday = "101"; 
       $scope.miqaat = obj; 
       console.log("Data from Service"); 
       console.log(obj); 
      } 
     }) 
     .catch(function(response) { 
      response.response = "Internal Server Error!"; 
     });; 
}); 
app.factory('getMiqaat', function($http) { 
    return { 
     findMiqaat: function(date) { 
      var dat = date; 
      console.log(dat); 
      return $http({ 
       url: 'miqaat.json', 
       method: 'GET', 
      }) 
     } 
    }; 
}) 

Demo

0

使用下面提及的代碼將正常工作。請確保按照您的json匹配鍵。

控制器代碼:

var app = angular.module("myApp", []); 

app.controller('myCtrl', function($scope, getMiqaat){ 
    $scope.date = "2017-04-11"; 
    console.log("date ::"+$scope.date); 
    $scope.status; 
    $scope.miqaat; 
    getMiqaatData(); 
    function getMiqaatData() { 
     getMiqaat.findMiqaat($scope.date) 
      .then(function (response) { 
       $scope.miqaat = response.data.response[0]; 
      }, function (error) { 
       $scope.status = 'Unable to load data: ' + error.message; 
      }); 
    } 

}); 

app.service('getMiqaat', function($http){ 

      this.findMiqaat = function(date){ 
       var dat = date; 
       console.log(dat); 
       return $http({ 
        url: 'miqaat.json', 
        method: 'GET', 
       }); 
       } 

}) 

Json : 

{ 
    "status":true, 
    "message":"List of the miqaats", 
    "response":[ 
    {"miqaat_date":"2017-04-11", 
     "mq_title":" New Year", 
     "isHoliday" :"101" 
    }, 
    {"miqaat_date":"2017-04-09", 
     "mq_title":"Some Fest", 
     "isHoliday" :"101" 
    } 
    ] 
} 
相關問題