简体   繁体   中英

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:

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).

Here are the two objects I am comparing when I log them in the console:

figure 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

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. 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.

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):

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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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