0

my code :

    app.factory('requestData',function($http,$q){
       return {
        reqData:function(params,url){
            return $http.get(url,{"params":params}).then(function(data){
                return data.data
            });
        });

    requestData.reqData( {module:"channel"},'/channel' ).then(function(data){
            $scope.selectChannel = data.value;
        })
     .then(function(){

         requestData.reqData( {channel_id:$scope.selectChannel},'/channel').then(function(data){
             $scope.summary = data.summary;
         })

      })
     .then(function(){
        //use $scope.summary do something
      });

firstly,i must set "$scope.selectChannel" value by asynchronous,then i use "$scope.selectChannel" to request. there is another method to achieve?

2
  • There is a mismatch of brackets in your code. What is the opening bracket for the one you are closing at line 7? Commented Oct 15, 2014 at 4:03
  • Did either of these answers solve your problem? Commented Oct 29, 2014 at 21:44

2 Answers 2

1

Try something like this:

app.factory('RequestData',function($http,$q) {
    var RequestData = {
        // assuming params is an object
        reqData: function(params) {
            $http.post('/channel', params).then(function(response){
                RequestData.selectChannel = response.value;
            });
        }
    };

    return RequestData;
});

In your controller, inject RequestData as a dependency then set $scope.RequestData = RequestData then you will have access to RequestData.selectChannel in your views.

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

Comments

1

I advice you to use $resource instead.

.factory("$$service", ["$resource", function ($resource) {
    return $resource('/channel',
        {
            module: "@module",
            channel_id: "@channel_id"
        };
}]);

and use in this way:

var service = new $$service();
service.$get({"module":"channel"})
    .then(function(data){
        return service.$get({"channel_id":data.value});
    })
    .then(function(data){
        $scope.summary = data;
    });

Comments

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.