Problems acessing a $resource variable

9 views
Skip to first unread message

Marcos Feijó Felipe

unread,
Jul 13, 2018, 1:47:12 AM7/13/18
to Angular and AngularJS discussion
I made a request for a php getting a JSON string, when I acess the string in my template using $ctrl.respostas[0].status it return to me a string with a value, but when I try to use the same model in my controller (this.respostas[0].status), it came nothing, I need to check the answer in my controller, but I don't know how, any one can help me?

Template: 
<p>respostas: {{$ctrl.response}}</p>
<p>confere: {{$ctrl.test}}</p>
<p>status: {{$ctrl.response[0].status}}</p>



Controller

angular.
module('tela').
component('tela', {
templateUrl: 'tela/tela.template.html',
controller: ['Request',
function telaController(Request, $rootScope) {
this.test = "ola";
this.id = "1";
this.validate = function validate() {
this.response= Request.save({id: this.id},[]);
this.test = "algo";
this.test = this.response[0].status;
}
}
]
});

Request
angular.
module('request.Request').
factory('Request', ['$resource',
function($resource) {
return $resource('./php/acessing.php',{
teste: "@id",
}, {
save: {
method: 'POST',
hasBody: true,
isArray: true,
cache: false
}
});
}
]);

Sander Elias

unread,
Jul 13, 2018, 2:46:54 AM7/13/18
to Angular and AngularJS discussion
Hi Marcos,

$resource methods are async. that means that the values are not provided directly, but after an unknown while, and certainly not before the next cycle. So, code following directly after a save|post|get|whatever will have no access to the result.
I don't really remember the specifics around this, but the promise itself is also exposed, and you can wrap the code you need inside a `.then(..)` to get access to the results when they are available.

Regards
Sander

Marcos Feijó Felipe

unread,
Jul 13, 2018, 7:56:36 AM7/13/18
to Angular and AngularJS discussion
Reply all
Reply to author
Forward
Message has been deleted
0 new messages