从控制器发送路径到服务,该服务将向服务器返回一个承诺

时间:2016-08-05 08:05:45

标签: angularjs ajax angular-ui-router angular-services

我有一个服务从文件中获取数据(路径由控制器提供)并返回一个promise - 然后是另一个使用上一个服务返回的数据创建具有属性的对象的服务。

我的问题是:

  1. getDataService 在控制器之前运行,因此它没有从中获取数据的路径=>没有任何回报(错误)
  2.   

    提供商' GetDataService'必须从$ get factory方法返回一个值。

    1. 我需要保留这种结构,因为我会有更多控制器使用不同的路径来提供
    2. 我也打开其他解决方案,但我需要确保在填充模板之前加载数据。我首先尝试使用getData服务调用SetProperties服务 - 但仍然首先执行getData.js
    3. getdata服务

      angular.module('myApp').factory('GetDataService',['$http', function($http) {
          var getData = function(path){
              return  $http.get(path).then(function(result) {
                  return result.data;
              });
          };
      }]);
      

      setProperties服务

      angular.module('myApp').service('PageProperties',['$http', function($http) {
      
          this.setProps = function(page, data) {      
      
                  some code here
      
              var properties = {
                      isCenterActive : isActive_val,
                      //header elements
                      titleClass : page, 
                      title : data.titles[page],
                      //footer elements
                      leftLink : leftLink_val,
                      leftFooterClass: leftLink_val,
                      leftTitle: data.titles[leftLink_val],
                      centerLink : centerLink_val,
                      centerFooterClass: data.titles[centerLink_val],
                      centerTitle : centerTitle_val,
                      rightLink : rightLink_val,
                      rightFooterClass: rightLink_val ,
                      rightTitle : data.titles[rightLink_val],            
                  }
      
              return properties;
          }
      }]);
      

      控制器

      angular.module('myApp', [])
      .controller('meniuController', ['$http', '$stateParams', '$scope', 'GetDataService', 'PageProperties', 
                  function($http, $stateParams, $scope, GetDataService, PageProperties){      
          var page = "meniu";
          $scope.language = $stateParams.lang;
          var path = '_global/views/services/json/' + $stateParams.lang + '_data.json';
      
          /*PageProperties.setProps(page, path).then(function(data){
              //some code here
          });*/
          GetDataService.getData(path).then(function(data){
              $scope.props = PageProperties.setProps(page, data);
          }).catch(function(){
              $scope.error = 'Unable to get data';
          });
      }])
      

      提前致谢!!

1 个答案:

答案 0 :(得分:0)

错误表明您的GetDataService提供商(定义为工厂)并未返回任何内容

angular.module('myApp').factory('GetDataService',['$http', function($http) {
    var getData = function(path){
        return  $http.get(path).then(function(result) {
            return result.data;
        });
    };

    // you need to actually return something

    return { getData: getData };
}]);

然后,您可以PageProperties使用GetDataService

angular
   .module('myApp')
   .service('PageProperties',['GetDataService', function(GetDataService) { 
      this.getProperties = function(path) {
         return GetDataService.getData(path).then(/*transform here*/)
      }
相关问题