简体   繁体   English

如何将promise的属性修改为等于AngularJS中的对象的属性?

[英]How can I modify the properties of a promise to be equal to the properties of an object in AngularJS?

Basically, I am trying to create a generic function in a service that takes the values of one object and checks to see with another object if any of its parameters are shared, and if they are, replaces the original object's parameter value with that of the shared parameter in the new object. 基本上,我试图在一个服务中创建一个泛型函数,该函数接受一个对象的值并检查是否与另一个对象共享任何参数,如果共享,则将原始对象的参数值替换为新对象中的共享参数。 This function works for any JSON object and looks like this: 此函数适用于任何JSON对象,如下所示:

load: function(loadTrue, settings1, settings2) {
  if (loadTrue) {
    for (var prop in settings1) {
      if (settings2.hasOwnProperty(prop)) {
        settings1[prop] = settings2[prop];
      }
    }
    return settings1;
  }
  else{
    return settings1;
  }
},

Like I said, this works perfectly fine for any JSON object, but the problem I'm facing now is when I try to compare a resource to an object (the second item in this function will always have to be a plain JSON object due to the nature of my project, but I can't always be returning an object, sometimes it is a resource that I am returning). 就像我说的那样,这对于任何JSON对象都可以很好地工作,但是我现在面临的问题是当我尝试将资源与对象进行比较时(由于以下原因,该函数中的第二项始终必须是普通的JSON对象)项目的性质,但我不能总是返回一个对象,有时它是我返回的资源)。

Here are the two objects I am comparing when I log them in the console: 这是我在控制台中登录它们时正在比较的两个对象:

figure 1 图1

Settings1: Resource {$promise: Object, $resolved: false}
Settings2: Object {class: "messageType", id: 181, message: "Test messageee"…}

However when expanded, the two look very similar: 但是,当展开时,两者看起来非常相似:

figure 2 图2

Settings1:
$promise: Object
$resolved: true
class: "messageType"
id: 181
message: "Test"
messageName: "testing 1"

Settings2:
class: "messageType"
id: 181
message: "Test messageee"
messageName: "testing 1"

As you can see, I basically need to overwrite the message property in settings1 to be that of settings2. 如您所见,我基本上需要将settings1中的message属性覆盖为settings2。 The problem seems to be (when going through the debugger), instead of going through figure[2], angualr seems to look through figure[1] and sees that there are no shared parameters between the two objects, and thus nothing is over written. 问题似乎是(通过调试器时),而不是通过Figure [2],Angualr似乎正在查看Figure [1],并且发现两个对象之间没有共享参数,因此没有任何覆盖。

The method call for this looks like: 为此的方法调用如下所示:

$scope.subject = myService.load($scope.loadIsTrue, $scope.subject, $localStorage.subject2);

Subject is declared like so: 主题声明如下:

$scope.subject = messageService.messageResource.get({messageName: 'test'});

Try these changes: 尝试以下更改:

In your messageService service (you have to change this to match your method): 在您的messageService服务中(您必须更改它以匹配您的方法):

get: function (message) {
            var deferred = $q.defer();

            $http({
                url: '/YourHTTPcall',
                method: "GET",
                dataType: 'json'
            }).success(function (data, status, headers, config) {                    
                deferred.resolve(data);                                                        
            }).error(function (data, status, headers, config) {

            });
            //This line is key
            return deferred.promise;
        }

Then in your other code making the calls 然后在您的其他代码中进行调用

messageService.messageResource.get({messageName: 'test'}).then(function(data){
   $scope.subject = data;
}).then(function() {
   myService.load($scope.loadIsTrue, $scope.subject, $localStorage.subject2);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM