angularjs - Angular Directive and $http get -


i'm struggling getting directive populate data api. if feed regular json works fine, if use http get blanks (the response fine in console).

what's best practice in scenario. found watch, seem pretty dirty, no? not quite sure how adheres scenario..

angular.module('contestantlist', [])   .directive('ccontestantlist', function() {     return {       scope: {},       templateurl: '/app/themes/pomgallery/contestant_list.html',       replace: true,       controller: 'contestantlistctrl',       controlleras: 'ctrl'     };   })   .controller('contestantlistctrl', function($scope, $http) {      $http({       method: 'get',       url: '/wp-json/posts',       params: {         'filter[posts_per_page]': 3       },     }).     success( function( data, status, headers, config ) {       this.contestants = data; /* not work */     }).     error(function(data, status, headers, config) {});      /* works */     //this.contestants = [{ "title": "nadine shjölin, solo exhibition"},{"title": "hello world!"}];    });  angular.module('pomgallery', ['contestantlist']); 

this doesn't have context of controller within success callback (different scope)

try storing in variable first.

.controller('contestantlistctrl', function($scope, $http) {     var vm = this; // store reference "this"     $http({       method: 'get',       url: '/wp-json/posts',       params: {         'filter[posts_per_page]': 3       },     }).     success( function( data, status, headers, config ) {       vm.contestants = data;     })...... 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -