NoName
NoName

Reputation: 181

Start several Functions by chain

I've got factory with some return on open modal window Example:

openModal: function (urn, id, templateId, controller) {
                $http({
                    method: 'GET',
                    url: urn
                }).success(function (data, $scope) {
                    $("#" + id).append($compile(data)($scope));
                    modalInstance = $uibModal.open({
                        templateUrl: templateId,
                        controller: controller,
                        backdrop: 'static',
                        keyboard: false
                    });
                }).error(function () {
                    alert("error");
                    return null;
                });
                document.getElementById('main').classList.add("blur");
            },

After this function and view append to DOM, I want to call another function from another service witch i inject to thouse openModalfactory.

Example:

$map.autocomplite('wizardsearch');
$map.getAutoUserLocationPath(); 

Could you explain how I must do this through $q? Thank you for you answer.

Upvotes: 0

Views: 62

Answers (2)

Peter Wilson
Peter Wilson

Reputation: 4319

deferring should make the function return a promise which you can use to do something when this function is finished look to the the following code

openModal: function (urn, id, templateId, controller,$q) {
            var def = $q.defer();
            $http({
                method: 'GET',
                url: urn
            }).success(function (data, $scope) {
                def.resolve(data);
                $("#" + id).append($compile(data)($scope));
                modalInstance = $uibModal.open({
                    templateUrl: templateId,
                    controller: controller,
                    backdrop: 'static',
                    keyboard: false
                });
            }).error(function (err) {
                def.reject(err)
                alert("error");

            });
            document.getElementById('main').classList.add("blur");
             return def.promise;
           }

to use it call it like :

openModel().then(function(){     
   //do what you want here
})

Upvotes: 1

geo
geo

Reputation: 2433

I imagine that you want something like this :

let deferred = this.$q.defer();
let promises = [];
promises.push(service.something);
this.$q.all(promises).then(/*Do something*/);
return deferred.promise;

Upvotes: 0

Related Questions