AngularJS Factory Parameter
Asked Answered
P

3

18

I'm trying to send a parameter to an angularjs service. Here is my service code :

angular.module('skyBiometryServices', ['ngResource'])
.factory('Facedetect', function( $resource ) {
    return $resource('skyBiometry/facedetect', {}, {
        query: {
            method : 'GET',
            params : {imageUrl: "http://cdn1-public.ladmedia.fr/var/public/storage/images/dossiers/presidentielles-2012/les-news-sur-les-presidentielles-2012/exclu-public-cauet-pour-ces-presidentielles-personne-ne-me-fait-rever-209063/2064021-1-fre-FR/Exclu-Public-Cauet-Pour-ces-presidentielles-personne-ne-me-fait-rever-!_portrait_w674.jpg"},
            isArray: false
        }
    })
});

In my controller i have this :

function IndexCtrl($scope,Facedetect) {
    $scope.text = Facedetect.query();
}

How can i send the imageurl into my services from the controller ? Something like this

function IndexCtrl($scope,Facedetect) {
    $scope.text = Facedetect.query('MY IMAGE URL');
}

In advance thanks.

Proctoscope answered 21/3, 2014 at 14:48 Comment(0)
P
0

With more research i found a solution :

factory('Facedetect', function( $resource ) {
    return $resource('skyBiometry/facedetect', {}, {
        query: {
            method : 'GET',
            params : {imageUrl: "http://cdn1-public.ladmedia.fr/var/public/storage/images/dossiers/presidentielles-2012/les-news-sur-les-presidentielles-2012/exclu-public-cauet-pour-ces-presidentielles-personne-ne-me-fait-rever-209063/2064021-1-fre-FR/Exclu-Public-Cauet-Pour-ces-presidentielles-personne-ne-me-fait-rever-!_portrait_w674.jpg"},
            isArray: false
        }
    })
});


function IndexCtrl( $scope, $routeParams, Facedetect ) {
    $scope.imageurl = 'http://flepi.net/images/personne-tendue.jpg';
    $scope.text = $scope.text = Facedetect.get({imageUrl: $scope.imageurl});
}

I don't know if it's the best way but it works.

Proctoscope answered 21/3, 2014 at 15:14 Comment(0)
A
23

You can write your factory like this

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

  return {

    query: function(image_url) {
      return $resource('skyBiometry/facedetect', {}, {
             query: { method: 'GET', params: {imageUrl:image_url}, isArray: false }
      }).query();

    }
  }
});

Now in your controller you can write

function IndexCtrl($scope, Facedetect) {
  $scope.text = Facedetect.query("YOUR/IMAGE/URL");
}
Arcadian answered 5/9, 2014 at 15:26 Comment(4)
Is there a way to NOT to write the function inline with the return object? keep it separated?Hamal
If I understood what you mean, you can define function outside the return statement, let's name it qfunc, now you can do something like this return { query: qfunc };Arcadian
Yeah, but what about the parameters?Hamal
The call still the same since you just moved the function Facedetect.query("YOUR/IMAGE/URL")Arcadian
S
14

If i understand correctly, you want something like that:

app.factory('myFactory',function(){

  return{

    prop: '',

    setProp: function(newProp){

        this.prop = newprop;

    }
  }

});

You should watch this:

https://egghead.io/lessons/angularjs-providers

And read this:

AngularJS: Service vs provider vs factory

Sulphanilamide answered 21/3, 2014 at 14:56 Comment(1)
Yes but with using ngresources.Proctoscope
P
0

With more research i found a solution :

factory('Facedetect', function( $resource ) {
    return $resource('skyBiometry/facedetect', {}, {
        query: {
            method : 'GET',
            params : {imageUrl: "http://cdn1-public.ladmedia.fr/var/public/storage/images/dossiers/presidentielles-2012/les-news-sur-les-presidentielles-2012/exclu-public-cauet-pour-ces-presidentielles-personne-ne-me-fait-rever-209063/2064021-1-fre-FR/Exclu-Public-Cauet-Pour-ces-presidentielles-personne-ne-me-fait-rever-!_portrait_w674.jpg"},
            isArray: false
        }
    })
});


function IndexCtrl( $scope, $routeParams, Facedetect ) {
    $scope.imageurl = 'http://flepi.net/images/personne-tendue.jpg';
    $scope.text = $scope.text = Facedetect.get({imageUrl: $scope.imageurl});
}

I don't know if it's the best way but it works.

Proctoscope answered 21/3, 2014 at 15:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.