javascript - Setting controller attributes inside ajax call -
i new in angularjs , i'm doubt controller attributes. created attribute called anuncio, attribute has array of objects showed in image bellow:
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 call webservice function loadanuncios , try set values directly using "this.anuncios" message "this.anuncios undefined". if create var called anuncs , set "this.anuncios = anucs", , instead of set ajax call directly this.anuncios set anucs image bellow, 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 works?
i might suggest going you're doing in different way.
var anunciomodule = angular.module('anunciomodule',[]); anunciomodule.controller('registrationcontroller', ['$scope', '$http', function ($scope, $http) { $scope.anuncios = []; $scope.loadanuncios = function() { $http.get('http://localhost:8080/pederofer/anuncios/get.action').then(function(result) { $scope.anuncios = result.data; }); }; }
i don't know if that's you're after maybe make lightbulb go off you.
Comments
Post a Comment