In your controller, just call
$http.get(url).then(function(data){$scope.myData =
data.data}).catch(function(err){console.log(err)});

In simple cases, I prefer just calling $http.get directly. In more
complicated cases, it makes sense to put that behind an abstraction layer
(if you're cache-ing calls, or you do a lot of hits on the same URL and
want to use singletons to keep control of which url gets hit, etc etc). But
that's just IMO. The important point (that a lot of people seem to miss in
the documentation) is that the .success and .error callbacks on $http are
not needed - the return values from $http calls are all promises which
conform to the standard spec, so you can just .then chain from there.

e


On Fri, Sep 5, 2014 at 12:23 PM, mark goldin <[email protected]> wrote:

> <return $http.get().then(function(data){return massageData(data)}).catch(
> function(err){at least you have some error handling})
> How do I consume that in my Controller if there is no function (getData())
> anymore?
>
>
> On Friday, September 5, 2014 2:14:22 PM UTC-5, mark goldin wrote:
>>
>> Jumped to conclusion too quickly. The file is in black now, but the error
>> is still there.
>>
>> On Friday, September 5, 2014 2:06:34 PM UTC-5, mark goldin wrote:
>>>
>>> That was it! Added it to my web.config and no more errors.
>>> Thanks for the help.
>>>
>>> On Friday, September 5, 2014 1:54:39 PM UTC-5, Thomas Murphy wrote:
>>>>
>>>> I'd be willing to put a bit of money on it throwing a CORS if you're
>>>> not running your angular app against a server.
>>>>
>>>>
>>>>
>>>> On Fri, Sep 5, 2014 at 2:50 PM, Eric Eslinger <[email protected]>
>>>> wrote:
>>>>
>>>>> If the chrome dev tools are showing a red status, then the error isn't
>>>>> in your promise handling, it's in the actual fetching of the file - that
>>>>> file isn't being properly served by your backend, or isn't being served 
>>>>> the
>>>>> way you think it is (you may need to set cookies or a token to access it,
>>>>> etc - it depends on your server setup).
>>>>>
>>>>> e
>>>>>
>>>>>
>>>>> On Fri, Sep 5, 2014 at 11:48 AM, mark goldin <[email protected]>
>>>>> wrote:
>>>>>
>>>>>> Tools will show the same info even if I try to access non existent
>>>>>> file.
>>>>>>
>>>>>>
>>>>>> On Friday, September 5, 2014 1:43:35 PM UTC-5, mark goldin wrote:
>>>>>>>
>>>>>>> The url is correct, I am not fully showing it.
>>>>>>> In the Tools the file is in Red.Status text Canceled. Can't see any
>>>>>>> more info about failure.
>>>>>>>
>>>>>>> On Friday, September 5, 2014 1:34:28 PM UTC-5, Eric Eslinger wrote:
>>>>>>>>
>>>>>>>> You can just treat $http.get() as a promise. You don't have to
>>>>>>>> directly instantiate a second promise unless you're doing something 
>>>>>>>> funky.
>>>>>>>>
>>>>>>>> return $http.get().then(function(data){return
>>>>>>>> massageData(data)}).catch(function(err){at least you have some
>>>>>>>> error handling})
>>>>>>>>
>>>>>>>> Also, if you're getting an error, what's the failed? At the very
>>>>>>>> least, I'm 99.9% sure that http://localhost:/localfile.txt is an
>>>>>>>> incorrect url. Don't you have to put a port after a : in a host 
>>>>>>>> definition?
>>>>>>>> so either localhost/localfile or localhost:3000/localfile. So it could 
>>>>>>>> be
>>>>>>>> that your $http.get is just failing. You can get more information on 
>>>>>>>> this
>>>>>>>> in the chrome debug tools - look at your network requests, and it 
>>>>>>>> should
>>>>>>>> show the localfile.txt XHR, and either be black (success!) or red 
>>>>>>>> (fails!)
>>>>>>>> and have more information there.
>>>>>>>>
>>>>>>>> e
>>>>>>>>
>>>>>>>>
>>>>>>>> On Fri, Sep 5, 2014 at 10:54 AM, mark goldin <[email protected]>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> Actually I am rather getting alert that says Error.
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> On Friday, September 5, 2014 12:52:33 PM UTC-5, mark goldin wrote:
>>>>>>>>>>
>>>>>>>>>> Ok, I have changed my code. Here it is:
>>>>>>>>>>
>>>>>>>>>> Service
>>>>>>>>>> angular.module('myModule').factory("service", function ($http,
>>>>>>>>>> $q) {
>>>>>>>>>>
>>>>>>>>>>     var requestUri = 'http://localhost:/localfile.txt';
>>>>>>>>>>     return {
>>>>>>>>>>         getData : function(){
>>>>>>>>>>             var deferred = $q.defer();
>>>>>>>>>>             var response = $http.get(requestUri);
>>>>>>>>>>             response.success(function (data) {
>>>>>>>>>>                 deferred.resolve(data);
>>>>>>>>>>             });
>>>>>>>>>>             response.error(function (data) {
>>>>>>>>>>                 alert('Error');
>>>>>>>>>>             });
>>>>>>>>>>             // Return the promise to the controller
>>>>>>>>>>             return deferred.promise;
>>>>>>>>>>         }
>>>>>>>>>>     }
>>>>>>>>>> });
>>>>>>>>>>
>>>>>>>>>> Controller:
>>>>>>>>>> var promise = service.getData();
>>>>>>>>>>     $scope.data = promise.then(function (data) {
>>>>>>>>>>         alert('Success: ' + data);
>>>>>>>>>>     }, function (reason) {
>>>>>>>>>>         alert('Failed: ' + reason);
>>>>>>>>>>     }
>>>>>>>>>>     );
>>>>>>>>>> When I run it I get the alert that says Failed ...
>>>>>>>>>>
>>>>>>>>>> BTW, if I change my service to this:
>>>>>>>>>> getData : function(){
>>>>>>>>>>             var deferred = $q.defer();
>>>>>>>>>>             deferred.resolve('1234');
>>>>>>>>>>             return deferred.promise;
>>>>>>>>>>         }
>>>>>>>>>> Then I get Success
>>>>>>>>>>
>>>>>>>>>> On Friday, September 5, 2014 11:32:42 AM UTC-5, Thomas Murphy
>>>>>>>>>> wrote:
>>>>>>>>>>>
>>>>>>>>>>> Great, so let's focus on what's happening in the controller. As
>>>>>>>>>>> Sander said, you're returning a promise from you service. *That
>>>>>>>>>>> promise needs to be resolved. *Check out the documentation here
>>>>>>>>>>> on the syntax, and see if it doesn't provide you the data you're 
>>>>>>>>>>> expecting.
>>>>>>>>>>> https://docs.angularjs.org/api/ng/service/$q
>>>>>>>>>>>
>>>>>>>>>>> Feel free to paste in a bit more of your controller code too.
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> On Fri, Sep 5, 2014 at 12:28 PM, mark goldin <[email protected]
>>>>>>>>>>> > wrote:
>>>>>>>>>>>
>>>>>>>>>>>> 1. serviceName.getData();
>>>>>>>>>>>> 2. The url points to a text file on the local web server.
>>>>>>>>>>>> Navigating from Browser to the file opens it with no problem.
>>>>>>>>>>>>
>>>>>>>>>>>> On Friday, September 5, 2014 9:58:27 AM UTC-5, Thomas Murphy
>>>>>>>>>>>> wrote:
>>>>>>>>>>>>>
>>>>>>>>>>>>> 1. What are you doing in the controller once you call
>>>>>>>>>>>>> serviceName.getData()?
>>>>>>>>>>>>> 2. What url are you calling in $http.get? If you're not
>>>>>>>>>>>>> calling anything, you'll get no data.
>>>>>>>>>>>>>
>>>>>>>>>>>>> Best,
>>>>>>>>>>>>> Thomas
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>> On Fri, Sep 5, 2014 at 10:49 AM, mark goldin <
>>>>>>>>>>>>> [email protected]> wrote:
>>>>>>>>>>>>>
>>>>>>>>>>>>>> I also found this code:
>>>>>>>>>>>>>>  function ($http, $q) {
>>>>>>>>>>>>>>   return {
>>>>>>>>>>>>>>         getData : function(){
>>>>>>>>>>>>>>             var deferred = $q.defer();
>>>>>>>>>>>>>>             var promise = $http.get(url).success(function
>>>>>>>>>>>>>> (response) {
>>>>>>>>>>>>>>                 deferred.resolve(response);
>>>>>>>>>>>>>>             });
>>>>>>>>>>>>>>             // Return the promise to the controller
>>>>>>>>>>>>>>             return deferred.promise;
>>>>>>>>>>>>>>         }
>>>>>>>>>>>>>>     }
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> and in controller to get data: serviceName.getData();
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> but I get no data. Can you please explain why is not working?
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> Thanks
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> On Friday, September 5, 2014 9:39:49 AM UTC-5, mark goldin
>>>>>>>>>>>>>> wrote:
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> Ok, I see how it works.
>>>>>>>>>>>>>>> Another question. Is it possible to have just one function 
>>>>>>>>>>>>>>> *MyTestService
>>>>>>>>>>>>>>> *and run it like this?
>>>>>>>>>>>>>>> <div ng-controller='*MyTestService *as vm'>
>>>>>>>>>>>>>>>        <p>the result from the service load:</p>
>>>>>>>>>>>>>>>        <pre>{{vm.data|json}}</pre>
>>>>>>>>>>>>>>>     </div>
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> On Friday, September 5, 2014 9:28:42 AM UTC-5, mark goldin
>>>>>>>>>>>>>>> wrote:
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> What vm.data shown in index.html would come from if I
>>>>>>>>>>>>>>>> remove the TestMyService function?
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> On Friday, September 5, 2014 1:32:11 AM UTC-5, Sander Elias
>>>>>>>>>>>>>>>> wrote:
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>> Hi Mark,
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>> You mean something like this: http://plnkr.co/edit/5cE
>>>>>>>>>>>>>>>>> NSfOAhKJDI0iwe6ZV?p=preview
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>> Regards
>>>>>>>>>>>>>>>>> Sander
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>  --
>>>>>>>>>>>>>> You received this message because you are subscribed to the
>>>>>>>>>>>>>> Google Groups "AngularJS" group.
>>>>>>>>>>>>>> To unsubscribe from this group and stop receiving emails from
>>>>>>>>>>>>>> it, send an email to [email protected].
>>>>>>>>>>>>>> To post to this group, send email to [email protected].
>>>>>>>>>>>>>> Visit this group at http://groups.google.com/group/angular.
>>>>>>>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>>  --
>>>>>>>>>>>> You received this message because you are subscribed to the
>>>>>>>>>>>> Google Groups "AngularJS" group.
>>>>>>>>>>>> To unsubscribe from this group and stop receiving emails from
>>>>>>>>>>>> it, send an email to [email protected].
>>>>>>>>>>>> To post to this group, send email to [email protected].
>>>>>>>>>>>> Visit this group at http://groups.google.com/group/angular.
>>>>>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>  --
>>>>>>>>> You received this message because you are subscribed to the Google
>>>>>>>>> Groups "AngularJS" group.
>>>>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>>>>> send an email to [email protected].
>>>>>>>>> To post to this group, send email to [email protected].
>>>>>>>>> Visit this group at http://groups.google.com/group/angular.
>>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>>
>>>>>>>>
>>>>>>>>  --
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "AngularJS" group.
>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>> send an email to [email protected].
>>>>>> To post to this group, send email to [email protected].
>>>>>> Visit this group at http://groups.google.com/group/angular.
>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>
>>>>>
>>>>>  --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "AngularJS" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>> To post to this group, send email to [email protected].
>>>>> Visit this group at http://groups.google.com/group/angular.
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>
>>>>  --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to