AngularJs在var result =中保存data.d.results

时间:2016-01-29 11:02:59

标签: angularjs rest http sharepoint

是否可以将$ HTTP(data.d.results)中的数据保存/替换为var Result =?

 .factory('ContactService', [function () {
      var factory = {};

      factory.getContacts = function () {
        return contactList;
      }

      // contact list, usually would be a separate database
      var contactList = [
        {id: 0, name: 'Ned Stark', email: 'ned@winterfell.com', phone: '123-456-7890', url: 'www.google.com', notes: 'Winter is coming.'},

        ];

      return factory;

以上示例有效! 但我想将data.d.results与结果放在

  

Var contactlist =

$http({

method: 'GET',

url: 'URL', 

headers: { "Accept": "application/json;odata=verbose" }

}).success(function (data, status, headers, config) {

$scope.InProgs = data.d.results;

})
.error(function (data, status, headers, config) {

});

1 个答案:

答案 0 :(得分:0)

然后,您应在set

中添加service方法

添加方法

factory.setContacts = function (contacts) {
    contactList = contacts;
}

并在您的http success函数中调用它

$http({

method: 'GET',

url: 'URL', 

headers: { "Accept": "application/json;odata=verbose" }

}).success(function (data, status, headers, config) {

$scope.InProgs = data.d.results;

ContactService.setContacts(data.d.results); //the call of the setter

})
.error(function (data, status, headers, config) {

});

您的服务应该是

.factory('ContactService', [function () {
  var factory = {};

  factory.getContacts = function () {
    return contactList;
  }

  factory.setContacts = function (contacts) {
    contactList = contacts;
  }

  // contact list, usually would be a separate database
  var contactList = [
    {id: 0, name: 'Ned Stark', email: 'ned@winterfell.com', phone: '123-456-7890', url: 'www.google.com', notes: 'Winter is coming.'},

    ];

  return factory;

提醒:不要忘记在控制器中导入您的服务