3

I am new to typescript/ecma6 and would like to write this angular controller in typescript:

.controller('DashCtrl', function($scope, wpFactory) {

$scope.posts = [];
$scope.images = {};

wpFactory.getPosts(3).then(function (succ) {
  $scope.posts = succ;
  angular.forEach(succ, function(value, index) {
    $scope.setUrlForImage(index, value.featured_image);
  });
}, function error(err) {
  console.log('Errror: ', err);
});

$scope.setUrlForImage = function(index, id) {
  wpFactory.getMediaDataForId(id).then(function (succ) {
    $scope.images[index] = succ.source_url;
  });
};

})

with my actual approach I have problems with the scope of the methods in the class:

class DashCtrl {

public $inject = ['wpFactory'];

posts: any[];
images: any[];

constructor(public wpFactory: any) {
  this.getPosts();
}
getPosts(){
  ... ?
}

setUrlForImage(succ:any, index:any, id:any){
  ... ?
}

}

The interesting part for me is how to develop the getPosts and the setUrlForImages method. Any suggestions would be appreciated.

3
class DashCtrl {

  public $inject = ['wpFactory'];

  posts: any[];
  images: any[];

  constructor(public wpFactory: any) {
    this.getPosts();
  }

  getPosts() {
    this.wpFactory.getPosts(3).then(succ => {
      this.posts = succ;
      angular.forEach(succ, (value, index) => {
        this.setUrlForImage(index, value.featured_image);
      });
    }, (err) => {
      console.log('Errror: ', err);
    });
  }

  setUrlForImage(succ:any, index:any, id:any) {
    this.wpFactory.getMediaDataForId(id).then(succ => {
      this.images[index] = succ.source_url;
    });
  }
}
7
  • 1
    Fix formatting? Also: maybe give some info about the => syntax in TypeScript. Also question from my part, don't you need something special in configuration to allow TS Classes that can access $scope with using this? Jan 21 '16 at 15:08
  • hello guys, thank you for your answers. unfortunately both solutions did not work. my problem with your actual code for this.setUrlImage in getPosts() is: ts2346 - the supplied parameters do not match any signature of call target. and the generated code my wanted id for the call of the rest-service is undefined.
    – jet miller
    Jan 21 '16 at 15:17
  • @froginvasion take a look at this stackoverflow.com/questions/25266555/… for your last question and developer.mozilla.org/en/docs/Web/JavaScript/Reference/… arrow functions (=>) Jan 21 '16 at 15:26
  • i think the 'this' from this.setUrlImage in getPosts() is not able to access the class... but i don't know how to do it.
    – jet miller
    Jan 21 '16 at 15:27
  • 'this' is links to current class instance in getPosts, and can access to setUrlForImage function if you call getPosts method from class instance like this: dashCtrlInstance.getPosts() Jan 21 '16 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.