1

Problem:

An object in the angular controller returns a reference variable instead of the value for the object property. When I call that property directly inside one of the controller's it returns correctly but when I do the same on the front end it returns the reference variable instead of the property value.

Edit function of the Lot Controller

this.edit = function(lotId){
    lotService.getById(lotId).then(function(response){
        console.log(response.data[0]);
        console.log(response.data[0].Lot.id);
        lotService.broadcastSubLotChange(response.data[0]);
            ...
    });
}

Edit function console logs

console logs

Notice the property value returned in the object is different than the value when called directly


Additional Information:

Lot Service broadcast function

 this.broadcastSubLotChange = function(subLot){
        this.sublot=subLot;
    }

Form Controller Broadcast Listener

$scope.$on('sublotChange', function() {
        $scope.lot=lotService.sublot;
        if($scope.lot.Lot==null){
            $scope.lot={'Lot':{'parentLot':{'Lot':{'id':lotService.lot.Lot.id}}}};
        }   

        console.log(lotService.sublot);
        console.log("sublot: "+$scope.lot.Lot.id);
        console.log("lot: "+$scope.lot.Lot.parentLot.Lot.id); 
  });

Form Controller Listener Logs

console logs

HTML call of angular variables

  • {{lot.Lot}} returns correctly other than "id":"{{lot.Lot.id}}"
  • {{lot.Lot.parentLot.Lot.id}} returns correctly 755
  • {{lot.Lot.id}} returns {{lot.Lot.id}}
2
  • 1
    This is all very weird. My best guess is this id property is overriden by the front-end as a plain string, when somewhere in your html you try to set it to "'{{lot.Lot.id}}'". And that weird behaviour with the object happens because it's evaluated at different time (later) than a directly logged property and by that time it gets overriden by the string from markup. Try commenting out html code and see the console logs again. Commented Jan 9, 2017 at 20:49
  • That was my first inclination and I tried that already, but after reading your response I dug a bit deeper. I found the issue in a sperate controller. It appears that the the model's $scope.id was binded through the service layer, thus modifying it in one controller changed it in the other without having to broadcast it. The problem code in the other controller: <form:input type="hidden" path="lot" ng-model="lot.Lot.id" value="{{lot.Lot.id}}" /> changed to: <form:input type="hidden" path="lot" value="{{lot.Lot.id}}" /> Thanks for the help. Commented Jan 9, 2017 at 22:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.