0

So i have a service mainService where this function that creates a promise is present:

    this.myPromise = function() {

        var deferred = $q.defer();
        var promise = deferred.promise;

        return promise;

    }

And i have a controller secondController where i want my code to run when the promise is resolved

mainService.myPromise()
.then(filterpanel)

function filterpanel(handle) { ... }

I also have a mainController where i want to run some code and when that code succeeds i want to resolve the promise in my service

function count (a * b) {

    if ((a * b) === 10) {

        mainService.myPromise('ok');

    }

}

I tried to change myPromise to something like this, but that doesn't seem to work. Any ideas how i can accomplish this?

    this.myPromise = function(data) {

        var deferred = $q.defer();
        var promise = deferred.promise;

        if (data) {

            deferred.resolve('ok');

        }

        return promise;

    }

EDIT: why i need this is because when my app loads, i do a connection through websocket to another application.

Communicating with that application happens in three steps

  1. Open a connection to the app
  2. Open a document
  3. Make an empty object

Every step returns a unique handle (ID) that handle is needed to go to the next step 1 > 2 > ...

After step 3 the unique id needs to be send to another controller.

My problem i have now is that the other controller secondController already starts before these 3 steps are finished, so i don't have the correct handle yet.

That's why i tought of making a promise so the secondController waits till the first controller has the correct ID.

Peter
  • 1,240
  • 2
  • 13
  • 22
  • I would try and not blur the state of a service across your controller and service. Generally service do something with data passed or get data. If you need an instance of a service half way through some computation which your controller needs to provide more info consider splitting them two actions out into separate individual process that function on their own and chain them. If not consider a factory method which returns a object that contain the state of your computation. Whats the need? All you appear to be doing is generating a promise? Why not just call your service with data from start – ste2425 Mar 16 '16 at 08:34
  • You don't have a single static promise in your code, but rather create a new one with every call to `myPromise`. That can't work. – Bergi Mar 16 '16 at 08:43
  • The need is that my secondController has to wait for running his code after a value is assigned in my mainController. my secondController needs that value in order to communicate with an API. So i think that a promise is the best solution? I'm new to AngularJS, in one of the tutorials i've read they are telling that you can communicate between controllers by a service. That is what i am trying to do now – Peter Mar 16 '16 at 08:46
  • It is necessary that you have specified in more detail, and exactly what you want to achieve. Extend your question. – Stepan Kasyanenko Mar 16 '16 at 10:22
  • Probably, you should use the **events**. See https://docs.angularjs.org/api/ng/type/$rootScope.Scope or http://stackoverflow.com/questions/14502006/working-with-scope-emit-and-on. – Stepan Kasyanenko Mar 17 '16 at 10:52

1 Answers1

0

If you do not go into the reasons why you need it, then the solution to your question there.

Live example on jsfiddle.

angular.module('ExampleApp', [])
  .controller('ExampleController', function($scope, mainService) {
    $scope.text = "ok";
    $scope.action = function() {
      mainService.resolve($scope.text);
    }
  })
  .controller('SecondController', function($scope, mainService) {
    $scope.subscribe = function() {
      mainService.myPromise().then(filterpanel);
    }

    function filterpanel(handle) {
      $scope.textFromExampleCtrl = handle;
      console.log('from SecondController', handle);
    }
  })
  .service('mainService', function($q) {
    var promise = null;
    var deferred = null;
    return {
      myPromise: function() {

        deferred = $q.defer();
        promise = deferred.promise;

        return promise;

      },
      resolve: function(val) {
        if (deferred)
          deferred.resolve(val);
      }
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app="ExampleApp">
  <div ng-controller="ExampleController">
   <input ng-model="text">
    <button ng-click="action()">
      Action
    </button>
  </div>
  <div ng-controller="SecondController">
    <button ng-click="subscribe()">
      run Promise
    </button>
    {{textFromExampleCtrl}}
  </div>
</div>
Stepan Kasyanenko
  • 3,176
  • 1
  • 15
  • 23