I'm new to AngularJS and I have a question about controller attributes. I've created an attribute called 'anuncio', which contains an array of objects as shown below:
var anuncioModule = angular.module('anuncioModule',[]);
anuncioModule.controller('RegistrationController',['$http',function ($http){
this.anuncios;
this.loadAnuncios = function loadAnuncios(){
$http.get('http://localhost:8080/pederofer/anuncios/get.action').then(function(result){
this.anuncios.push.apply(this.anuncios, result.data);
});
}
}]);
When I call my web service with the function loadAnuncios and try to set the values directly using "this.anuncios", I receive the message "this.anuncios is undefined". But if I create a variable called 'anuncs' and set "this.anuncios = anucs", and instead of setting my AJAX call directly into this.anuncios, I set it to anucs like in the code snippet below, it works.
var anuncioModule = angular.module('anuncioModule',[]);
var anuncs = [];
anuncioModule.controller('RegistrationController',['$http',function ($http){
this.anuncios = anuncs;
this.loadAnuncios = function loadAnuncios(){
$http.get('http://localhost:8080/pederofer/anuncios/get.action').then(function(result){
anuncs.push.apply(anuncs, result.data);
});
}
}
My question is, why does this method work?