Question

I have this function that adds data to a $scope object.

function newUT(unit, apply) {
    newGuid(function (data) {
        var newTask = {
            id: data,
            TaskName: 'New Task',
            _showdetails: true,
        };
        unit.tasks.push(newTask);
        apply();
    });
}

This function is assigned to $scope in the controller by $scope.newUT = newUT;

Before I put in the array object, the ng-repeat wasn't updating. newGuid is a legacy app that is used to get Guids from the server, and put then in the local filestore Guid index table. So I cannot quite eliminate that code.

This code is called by:

<button type="button" ng-click="newUT(unit,$apply)">

So the issue is that I really don't like passing $apply, but if I don't I cannot get the array to update.

  1. Is there a better way?
  2. I saw Angular service containing http get call not working with ng-repeat
    1. But I'm not sure how I would wrap my newGuid call for a q call
    2. Since newGuid is a shared server call, I don't know if that is even the right way.

Is there a better way? Is what I am doing acceptable?

Was it helpful?

Solution

The $timeout service can help here. If you set up a wrapper service for your newGuid method then you can wrap the callback in a $timeout:

JSFiddle

app.service('svc', function($timeout){
    this.getGuid = function(cb){
        getGuid(function(value){
            $timeout(function(){
                cb(value);
            });
        });
    };
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top