0

I have been working on an angularjs webapp using WS of moodle to get information, I have a little problem with variables, I would like to get a variable from a $http. and I saw that you can use services as an option or $rootscope, but for me have not worked it.

I have two controller and i would like to pass $scope.userid to cursostCtrl

app.controller('userCtrl', function ($scope, $http) {



    field = '&field=username';  
    values = '&values[0]=adminaccap'/*+$scope.username*/;


    url = concatUrl + 'core_user_get_users_by_field' + field + values;


    $http.get(url).then(function (response) {
        $scope.items = response.data;
        $scope.userid = response.data[0].id;
    })

    return  $scope.userid;

});


app.controller('cursostCtrl', function($scope, $http){

    url2 = concatUrl + 'core_enrol_get_users_courses' + '&userid=' + $scope.userid;

    $http.get(url2).then(function (response) {
        $scope.cursos = response.data;
        $scope.nombrecurso = response.data[0].fullname; 
    })

Thanks for your help!

2
  • try with $rootScope,you can use a services also or some kind of store methods Commented Nov 24, 2016 at 13:11
  • I tried but I think $http. is not returning $scope.userid Commented Nov 24, 2016 at 13:28

2 Answers 2

2

The way forward here is to broad cast a event from one controller and handle the event on other controller

On your userCtrl broadCast an event to the rootScope as follows

   app.controller('userCtrl', function ($rootScope,$scope, $http) {

        field = '&field=username';  
        values = '&values[0]=adminaccap'/*+$scope.username*/;
        url = concatUrl + 'core_user_get_users_by_field' + field + values;

        $http.get(url).then(function (response) {
            $scope.items = response.data;
            $scope.userid = response.data[0].id;
            $rootScope.$broadcast("myBroadCastedEvent",{myUser:$scope.userid});
        })

        return  $scope.userid;

    });

Also in your cursostCtrl get the broadcasted event scope in as follows

app.controller('cursostCtrl', function($scope, $http){
  $scope.$on("myBroadCastedEvent", function (event, args) {
               $scope.userid = args.myUser;
           })

    url2 = concatUrl + 'core_enrol_get_users_courses' + '&userid=' + $scope.userid;

    $http.get(url2).then(function (response) {
        $scope.cursos = response.data;
        $scope.nombrecurso = response.data[0].fullname; 
    })
Sign up to request clarification or add additional context in comments.

5 Comments

I'm still getting $scope.userid as undefined.
If i do an alert inside $scope.on I'm getting $scope.userid but in the url2 is undefined. is there a way to get it out of $scope.on ?
Check the latest edit.Also check if the first controller's $http service on sucess returns a proper value of userid
inside of $scope.on Im getting $scope.userid but in the url2 is not defined
I have to put $http. inside the $scope.on rhen it work perect, thank you!
2

You can broadcast an event after http success like follows -

In controller 1 -

 $http.get(url).then(function (response) {
        // assuming you need response in another controller
        $rootScope.$broadcast('GotResponse',response);
    })

In controller 2 -

$scope.$on('GotResponse', function(event,response){
    // this variable response is what you got from http
    console.log('response--',response);
});

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.