AngularJS资源服务

时间:2017-12-01 13:06:28

标签: javascript angularjs angularjs-resource sharepoint-list

我是AngularJS的新手,正在尝试使用资源服务从REST服务中获取数据。我的问题是,当我尝试从两个不同的SharePoint列表中获取我的项目时,它只需要前两个项目字段。如果我但是$ select ='*'(假设从列表中获取所有项目)它只需要前两列(第一列中有三列,第二列中有七列)。这是我的代码:

categories.js的模型

var spa = spa || {};
spa.models = spa.models || {};

spa.models.categories = function () {
    this.Title = undefined;
    this.CategoryID = undefined;
    this.Description = undefined;
    this.__metadata = {
         type: 'SP.Data.CategoriesListItem'
    };
};

products.js的模型

var spa = spa || {};
spa.models = spa.models || {};

spa.models.products = function () {
    this.ID = undefined;
    this.Title = undefined;
    this.ProductID = undefined;
    this.Category = undefined;
    this.Price = undefined;
    this.AlcoholStrength = undefined;
    this.Content = undefined;

    this.__metadata = {
        type: 'SP.Data.ProductsListItem'
    };
};

datacontext.angular.js

(function () {
    'use strict';

//Defining service
var serviceId = 'datacontext';
angular.module('app').factory(serviceId,
    ['$rootScope', '$http', '$resource', '$q', 'config', 'common', 
           'spContext', datacontext]);

// Creating factory
function datacontext($rootScope, $http, $resource, $q, config, common, 
           spContext) {


    // Init service
    init();

    // Service signature
    return {
        getCategoriesPartials: getCategoriesPartials,
        getProductsPartials: getProductsPartials

    };

    function init() {
        common.logger.log("Service Loaded", null, serviceId);
    }

    function getCategoriesResource() {
        return $resource('_api/web/lists/getbytitle(\'Categories\')/items',
            {},  
            {
                get: {
                    method: 'GET',
                    params: {
                        '$select': '*'
                    },
                    headers: {
                        'Accept': 'application/json;odata=verbose'
                    }
                }
            });     
    }

    function getProductsResource() {
        return $resource('_api/Web/Lists/GetByTitle(\'Products
                      \')/Items?$top=10',
            {},
            {
                get: {
                    method: 'GET',
                    params: {
                        '$select': '*'

                    },
                    headers: {
                        'Accept': 'application/json;odata=verbose'
                    }
                }
            });  
    }

    function getCategoriesPartials() {
        var resource = getCategoriesResource();
        var deferred = $q.defer();

        resource.get({}, function (data) {
            deferred.resolve(data.d.results);
            common.logger.log("Retrived Categories partials", data, 
                   serviceId);
        }, function (error) {
            deferred.reject(error);
            common.logger.logError("Error when retrieved categories  
                   partials", error, serviceId);
            });
        return deferred.promise;

    }

    function getProductsPartials() {
        var resource = getProductsResource();
        var deferred = $q.defer();

        resource.get({}, function (data) {
            deferred.resolve(data.d.results);
            common.logger.log("Retrived Products partials", data, 
                   serviceId);
        }, function (error) {
            deferred.reject(error);
            common.logger.logError("Error retrieved Products partials",
                  error, serviceId);
        });
        return deferred.promise;
    }

}

})();

app.js

(function () {
    'use strict';

    // create app
    var app = angular.module('app', [
       // ootb angular modules
      'ngRoute',      
      'ngCookies',    
      'ngSanitize',      
      'ngAnimate',    
      'ngResource',


    // my custom modules
    'common'
]);

   // startup code
  app.run(['$route', 'angular.config', function ($route, angularConfig) {
  }]);
})();

Categories.js

(function () {
   'use strict';

    // define controller
    var controllerId = "categories";
    angular.module('app').controller(controllerId, ['$location', 
           '$routeParams', 'common','datacontext', categories]);

   // create controller for categories
   function categories($location, $routeParams, common, datacontext) {
   var vm = this;


     // init controller
     init();

     getCategories();



    // init controller
    function init() {
        common.logger.log("Controller Loaded", null, controllerId);
        common.activateController([], controllerId);        
    }

    function getCategories() {
        common.logger.log("In GetCategories", null, controllerId);
        datacontext.getCategoriesPartials()
            .then(function (data) {
                if (data) {
                    vm.categories = data;
                    common.logger.log("vm.categories Check", null,
                          controllerId);
                } else {
                    throw new Error('Error obtaining data in 
                          categories.js');
                }
            }).catch(function (error) {
                common.logger.logError('Error obatining categories items', 
                      error, controllerId);
            });
       }


    }

})();

请帮帮我!如果我只在datacontext.angular.js中有$ select ='Title,CategoryID',那么一切都很好。但是当我添加Description($ select ='Title,CategoryID,Description')时,程序直接进入common.logger.logError(“检索类别部分时出错”)。

我做错了什么?

由于

0 个答案:

没有答案