0

I have this code in my service.js

function myService($http, $log) {

 var API = 'https://myapi.net/api/api.php?';
 $log.debug(API);

 return {

  getApi: function(inputValue, inputValue2) {
    return $http({
      url:API + inputValue + '=' + inputValue2,
      method:'GET'
    })
  }
 };
}

and in my controller:

vm.searchNetflix = function() {
  netflixService.getApi(vm.searchResult, vm.searchResult2).then(function(result){
    vm.ResultSearch = result.data;
    $log.log('result' + result);
  });
};

This 2 values "SearchResult and SearchResults" I call in input, and change the api url,

example: https://myapi.net/api/api.php?mySearchResult=mySearchResult2

when I type a value it returns something right, and if I type something not valid, returns 404, or 400, if a of values is empty.

How I can valid this status, and return in my view?

Thks :)

2
  • So you're looking to capture an error state from the result of your service? Commented Mar 12, 2016 at 22:43
  • Yes, to be able to treat and give a result in the view, if my value is in wrong inputs Commented Mar 12, 2016 at 22:46

1 Answer 1

1

Relaying the information back to your view largely depends on how your view is constructed, but the principle is to ensure that you're catching the error condition correctly.

This is similar to an example on $http in the docs; you need to supply another method to your promise to cover the error conditions.

vm.searchNetflix = function() {
  netflixService.getApi(vm.searchResult, vm.searchResult2)
    .then(function(result) {
      vm.ResultSearch = result.data;
      $log.log('result' + result);
    }, function(errorResult) {
      // extract information from the errorResult here
    });
};

Any information you need to determine why the state is in error should ideally be provided in the errorResult object.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, you save me! :)

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.