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.

1 Answer 1

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;
    });
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

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?
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.
@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 (=>)
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.
'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()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.