Re: [angular.js] Bind Search service output

212 views
Skip to first unread message

Pawel Kozlowski

unread,
Jul 26, 2012, 1:11:40 AM7/26/12
to ang...@googlegroups.com
hi bsr!

It is hart to see exactly what is going on without having a live,
running code (jsFiddle would be of great help here!) but just looking
at your example it seems like you are trying to access a service
directly in your template. This won't work since in a template you've
got only access to things defined in a scope.

I don't know if the above explanation makes sense to you so please
send a jsFiddle if you need more help.

Cheers,
Pawel

On Wed, Jul 25, 2012 at 9:56 PM, bsr <bsr...@gmail.com> wrote:
> Hello,
>
> I am not sure this is the best way to do this, but I wrote a service to get
> search response from server.
>
> angular.module('xxx', []).service('Search', function($http) {
> this.find = function(q) {
> $http({method: 'GET', url:'/api/search', params:{ q: q}})
> .success(function (data) {
> return data;
> });
> };
> });
>
> I am calling this from view.
>
> <form class="form-search">
> <input type="text" class="search-query" placeholder="Search"
> ng-model="q">
> </form>
> <a class="btn" ng-click="result =Search.find(q)" href="">Search</a>
>
> Now,
> Search.find(q) does send http GET request, and I can see the response. But
> not sure how to assign the return value to a variable (say result) of scope
> ($scope. result).
>
> Is there a better way I should approach this. I did not use resource because
> I don't want to send a request, if user does not use Search functionality.
> and I may only do search (no save, update etc)
>
> thanks
>
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/angular/-/Q8HrthZDtL0J.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/angular?hl=en.

bsr

unread,
Jul 26, 2012, 7:56:11 AM7/26/12
to ang...@googlegroups.com
Thanks Pawel.

Please find the fiddle http://jsfiddle.net/bsr203/5zR5v/1/

I am not sure this is the best way to do it, but this is the requirement.

When the user clicks on search, I send a request with $http (this works).
I need to display the result in a table. (how??)

<pre>{{Search.find(q)}}</pre>
is just to show that it can invoke Search, but I only wanted to send the request when I click Search button.

thanks again for your help.
bsr.

Pawel Kozlowski

unread,
Jul 26, 2012, 2:18:06 PM7/26/12
to ang...@googlegroups.com
Hi!

On Thu, Jul 26, 2012 at 1:56 PM, bsr <bsr...@gmail.com> wrote:

> When the user clicks on search, I send a request with $http (this works).
> I need to display the result in a table. (how??)
>
> <pre>{{Search.find(q)}}</pre>
> is just to show that it can invoke Search, but I only wanted to send the
> request when I click Search button.

I guess this is what you want:
http://jsfiddle.net/pkozlowski_opensource/5zR5v/2/
Let me know if it works for you or if you need more help.

Cheers,
Pawel

bsr

unread,
Jul 26, 2012, 2:47:31 PM7/26/12
to ang...@googlegroups.com
Thanks Pawel,

I couldn't believe, it didn't work when I tried. I thought I may have to explicitly bind the 'result' variable.

Thanks again for your help.
Reply all
Reply to author
Forward
0 new messages