Pass scope to Service on AngularJS

Felix

I'm pretty new to AngularJS, I want to pass the scope to a service so I can perform a tag search based on the scope.value.

<div data-ng-app="instaSearch" data-ng-controller="search">

  <div>
    <input type="text" value={{value}} data-ng-model='value'/>
  </div>

 <p data-ng-hide="value">type a tag</p>
 <p data-ng-show="value">...looking for {{value}}</p>


<ul>

  <li data-ng-repeat="r in results">
    <a>
      <img ng-src="{{r.images.thumbnail.url}}"  alt="" />
    </a>
  </li>

</ul>  

</div>

Here is the JS

var app = angular.module('instaSearch', ['ngResource']);

app.factory('instagram', function($resource){

    return {
    searchTag: function(callback){

      var api = $resource('https://api.instagram.com/v1/tags/:tag/media/recent?client_id=:client_id&callback=JSON_CALLBACK',{
                client_id: '3e65f044fc3542149bcb9710c7b9dc6c',
        tag:'dog'
            },{
                fetch:{method:'JSONP'}
            });

            api.fetch(function(response){
                callback(response.data);

            });
    }
    }

});

app.controller('search', function($scope, instagram){

  $scope.$watch('value', function(){
    $scope.results = [];

    instagram.searchTag(function(data){
      $scope.results = data;
    });
  });

});

working example

zs2020

You can access the value using $scope.value.

app.factory('instagram', function ($resource) {
    return {
        searchTag: function (tag, callback) {
            var api = $resource('https://api.instagram.com/v1/tags/:tag/media/recent?client_id=:client_id&callback=JSON_CALLBACK', {
                client_id: '3e65f044fc3542149bcb9710c7b9dc6c',
                tag: tag
            }, {
                fetch: {
                    method: 'JSONP'
                }
            });

            api.fetch(function (response) {
                callback(response.data);

            });
        }
    }

});

app.controller('search', function ($scope, instagram) {
    $scope.$watch('value', function () {
        $scope.results = [];
        instagram.searchTag($scope.value, function (data) {
            $scope.results = data;
        });
    });
});

Demo: http://codepen.io/anon/pen/GHbIl

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pass scope to Service on AngularJS

From Dev

AngularJs: pass $scope variable with service

From Dev

AngularJS using service to pass data without $scope

From Dev

Is it improper, and if so why, to pass $scope to a service in Angularjs?

From Dev

how do i pass scope from controller to service in angularjs?

From Dev

Is $scope a real service in AngularJS

From Dev

AngularJS $scope inheritance service

From Dev

what is the scope of a service in angularjs?

From Dev

$scope is not defined in service AngularJs:

From Dev

AngularJS $scope inheritance service

From Dev

Is it bad practice to pass $scope to a service?

From Dev

AngularJS pass variable to service

From Dev

Pass a variable to a service with AngularJS?

From Dev

Service functions outside Angularjs scope

From Dev

Scope is lost in AngularJS Service callback

From Dev

Service functions outside Angularjs scope

From Dev

How do I pass $scope to service

From Dev

Angular Service - Pass $http data to scope

From Dev

Pass $scope value from controller to a service function

From Dev

Pass in current model/scope to AngularJS directive

From Dev

AngularJS pass a scope variable name as a function parameter

From Dev

Pass $scope along with $location.Path() in AngularJS

From Dev

AngularJS : Pass $scope variable as directive attribute

From Dev

AngularJS: When to pass $scope variable to function

From Dev

Pass variable to AngularJS directive without isolated scope

From Dev

Angularjs - To pass scope, from modal bootstrap window

From Dev

AngularJS pass filtered value to scope value

From Dev

How to pass $scope value to custom filter in AngularJs?

From Dev

AngularJS Service Undefined: Unknown provider: $scopeProvider <- $scope

Related Related

HotTag

Archive