angular js - 控制器使用的服务中需要的ajax调用结果

时间:2016-08-04 12:09:18

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

我需要一种方法在服务中使用json检索的数据,控制器使用该服务使用服务返回的值填充模板。

我想要做的就是用所选语言的内容填充模板,这些内容会因用户选择而改变(我现在很远,但这是一步)

控制器

(function(){
angular.module('myApp', [])
.controller('meniuController', ['$http', '$stateParams', '$scope', 'GetDataService', 'PageProperties', 
            function($http, $stateParams, $scope, GetDataService, PageProperties){      
    var page = "meniu";
    var path = '_global/views/services/json/' + $stateParams.lang + '_data.json';

    GetDataService.getData(path).then(function(result){         
        $scope.props = PageProperties.setProps(page, result.data);
    });
}])})();

GetDataService

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

                  function (httpError) {

                     throw httpError.status + " : " + 
                           httpError.data;
                  });
        }
   }
}]);})();

PageProperties服务

(function(){
angular.module('NimbusApp').service('PageProperties',['$http','GetDataService', function($http, GetDataService) {

    this.setProps = function(page, data) {                      

        //here I want to access returned data from getdataService
        var properties = {
                isCenterActive : isActive_val,
                titleClass : page, 
                title : data.titles[page],
                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-1.5.8.js:13920 SyntaxError:位置4的JSON中出现意外的标记t       at Object.parse(native)       at fromJson(http://localhost:8080/_global/lib/js/angular-1.5.8.js:1333:14)       在defaultHttpResponseTransform(http://localhost:8080/_global/lib/js/angular-1.5.8.js:10635:16)       在http://localhost:8080/_global/lib/js/angular-1.5.8.js:10726:12       at forEach(http://localhost:8080/_global/lib/js/angular-1.5.8.js:321:20)       在transformData(http://localhost:8080/_global/lib/js/angular-1.5.8.js:10725:3)       在transformResponse(http://localhost:8080/_global/lib/js/angular-1.5.8.js:11577:21)       at processQueue(http://localhost:8080/_global/lib/js/angular-1.5.8.js:16383:28)       在http://localhost:8080/_global/lib/js/angular-1.5.8.js:16399:27       在Scope。$ eval(http://localhost:8080/_global/lib/js/angular-1.5.8.js:17682:28)(anonymous函数)@ angular-1.5.8.js:13920(匿名函数)@ angular-1.5.8.js:10467processQueue @ angular-1.5.8.js:16391(匿名函数)@ angular-1.5.8.js:16399 $ eval @ angular-1.5.8.js:17682 $ digest @ angular-1.5.8.js:17495 $ apply @ angular-1.5.8.js:17790done @ angular- 1.5.8.js:11831completeRequest @ angular-1.5.8.js:12033requestLoaded @ angular-1.5.8.js:11966

抛出异常

  

angular-1.5.8.js:13920 undefined:undefined

这是正确的方法吗?因为似乎在getDataService

成功结果之前没有值呈现模板

0 个答案:

没有答案
相关问题