1

I have defined a function in my controller which calls a rest web-service in AngularJs. I need to assign the value returned by the function to a variable. The function may be called many times with different parameters and assign the value returned to different variables.

The function in my controller takes a parameter, which in turn is then passed to the web-service.

function disableBlockFn(blocKName) {

        var data = {
            formId: vm.formId,
            blockId: blocKName,
            statusId: vm.statusId,
            roleId: vm.roleId
        }

        roleService.displayBlock(data).then(function (data) {
            return data[0];
        });
    }

When i assign the value of the aboved function to a variable and print it on the console, i get 'Undefined'.

vm.pilot = vm.disableBlockFn('pilot_mpr_qualite_1');
console.log('pilot ', vm.pilot);

vm.pilot_ang = vm.disableBlockFn('pilot_mpr_qualite_ang');
    console.log('pilot ', vm.pilot_ang);

Function to request WS

 function displayBlock(objBlock) {
    var defer = $q.defer();
    $http({
        method: 'GET',
        url: config.urlPath + 'api/block/block'
    }).then(function (data) {
        var results = data;
        defer.resolve(results.data.data);
    }, function (data, status) {
        defer.reject({
            data: data,
            status: status
        });
    });

    return defer.promise;
}

1 Answer 1

1

You need to return a promise from disableBlockFn. If your function is in your controller, you can set the value directly.

function disableBlockFn(blocKName) {
    var data = {
        formId: vm.formId,
        blockId: blocKName,
        statusId: vm.statusId,
        roleId: vm.roleId
    }
    //here you can return only your promise
    return roleService.displayBlock(data);
}

And then, resolve the promise in your controller:

vm.disableBlockFn('pilot_mpr_qualite_1').then(function(data) {
    vm.pilot = data;
    console.log('pilot ', vm.pilot);
});

EDITED: If your function is in your controller you can do this:

function disableBlockFn(blocKName) {
    var data = {
        formId: vm.formId,
        blockId: blocKName,
        statusId: vm.statusId,
        roleId: vm.roleId
    }

    return roleService.displayBlock(data)
        .then(function(data){
            vm.pilot = data[0];
        });
}
Sign up to request clarification or add additional context in comments.

7 Comments

The function disableBlockFn(blocKName) is defined in my controller. I will update my question
I will need to call the function disableBlockFn(blocKName) with different parameters and assign each call to a different variable.
Then the first option is better. You can only return a promise from your function and on every call your can resolve using "then" and assign the returned value to any variable.
You can return in both. In your service and in your controller. But, the better way to use this, is your promise deal with all the logic. If you are returning a promise from your service, you can use in this way(like in the first part of the awnser) return roleService.displayBlock(data); So you just call the disableBlockFn every time you need and use then to assign the returned value to any variable.
As it is a asynchronous request, you can't have the value immediately after call your function. When you call "vm.test();", you need to wait the request finish. You can put your logic in the callback function: vm.disableBlockFn('pilot_mpr_qualite_1').then(function (data) { vm.pilot = data; console.log(vm.pilot) })
|

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.