0

I have an angularjs app with the following factory method that does live API call:

//Return SP json by SPID
    azureMobileClient.getSPjsonBySPID = function (callback, spid)
    {
        azureMobileClient.azureMSC.invokeApi("get_spjson_byspid", {
            parameters: {
                spid: spid
            },
            method: "get"
        }).done(function (results) {
            //console.log(results.result);

            callback(results.result);
        }, function (error) {
            alert(error.message);
        });
    }

And I have the following controller's method that consume it

$scope.getSPName = function (spid)
    {

        console.log("SPID:", spid);

        var returnedName;

        AzureMobileClient.getSPjsonBySPID(function (item)
        {
            console.log("item.fullname:", item.fullname);
            returnedName = item.fullname;


        }, spid);

        return returnedName;
    }

And this is how I am currently attempting to bind the returned item.fullname in my view (inside ng-repeat):

<p>
{{getSPName(t.parsedjsondata.SPId)}}
</p>

The problem is that the binding does not work although I do see the value of the console.log() with the correct item.fullname. I am guessing that this has to do with the delay of the API call and I need to insert $scope.$apply() somewhere but I am not sure what to do.

1

2 Answers 2

2

There are some bad practices in your code. Some suggestions:

  1. Use promises, i.e. $q in your factory methods.
  2. Try not to use expensive functions in your expressions {{}}. They are evaluated often as part of Angular's dirty checking process.

One way to solve your problem is to change your Angular service function to return a promise:

azureMobileClient.getSPjsonBySPID = function (spid)
{
    var deferred = $q.defer();
    azureMobileClient.azureMSC.invokeApi("get_spjson_byspid", {
        parameters: {
            spid: spid
        },
        method: "get"
    }).done(function (results) {
        //console.log(results.result);
        deferred.resolve(results.result);
    }, function (error) {
        alert(error.message);
        deferred.reject(error);
    });
    return deferred.promise;
}

Then in your controller do something like:

$scope.spName = null;
azureMobileClient.getSPjsonBySPID(spid).then(function(item){
    $scope.spName = item.fullname;
}, function(error) {
    // Show error
});
Sign up to request clarification or add additional context in comments.

Comments

0

Try this, lookslike you are binding wrongly {{}} use variable to resolve in value.

<p>
{{SPName}}
</p>

$scope.getSPName = function (spid)
    {

        console.log("SPID:", spid);

        var returnedName;

        return AzureMobileClient.getSPjsonBySPID(function (item)
        {
            console.log("item.fullname:", item.fullname);
            returnedName = item.fullname;
            return returnedName;
        }, spid);


    }
$scope.SPName = $scope.getSPName(t.parsedjsondata.SPId)

1 Comment

Still does not work. I can see the value of item.fullname being retrieved in the console.log but it is not being binded to the view. I have am sure that is because the value is retrieved from a live server with some delay and I need to call $scope.$applay() somehow but I am not sure how to call it with a 'return' statement in my method

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.