0

I followed documentation to set a Ionic popup in my project. This works well except one thing.

When I call the function, I'm not in controller (I'm in services.js), so when I define scope: $scope, I have an obvious $scope is unknown error.

How do you solve this? Thanks a lot !

Find bellow the full code if you need it.

controllers.js

$scope.confirm_redeem_reward = function(asked_reward) {
      RewardModel.displayConfirmRedeemReward(asked_reward)
    };

services.js

...

  RewardModel.displayConfirmRedeemReward = function(asked_reward) {
     var confirmPopup = $ionicPopup.confirm({
       title: 'Confirmation',
       templateUrl: 'templates/popups/confirm_redeem_reward.html',
       scope: $scope,
       buttons: [{
          text: 'Cncel',
          type: 'button-default'
        }, {
          text: 'OK',
          type: 'button-positive',
          onTap: function(e) {
            RewardModel.useReward(asked_reward.id);
            RewardModel.used_reward_or_deal_name = asked_reward.name;
          }
        }]

     });
     confirmPopup.then(function(res) {
       // Nothing
     });
    };

confirm_redeem_reward.html

Do you want to redeem the reward :
{{asked_reward.name}} ?

4
  • Passing $scope as a parameter of RewardModel.displayConfirmRedeemReward ? Commented Oct 24, 2014 at 0:19
  • I was told it's a bad practice ("Treat scope as read only in templates, Treat scope as write only in controllers" from the Zen Of Angular). I tried anyway and it does not seem to work (no error in console but result is not displayed) Commented Oct 24, 2014 at 0:32
  • Hi, did you solve this problem or any workarouds? Commented Nov 5, 2014 at 21:35
  • In the end, I used controller to write my popups :( Commented Nov 6, 2014 at 9:28

2 Answers 2

1

I ran into the same dilemma, whether to use a controller or a service. It feels to me that a service is a slightly better solution, since the controller for the popup is already wrapped within ionicPopup function. (I.e. i'm not writing the controller, i just delegate to ionicPopup) So I fixed it for now by injecting rootScope into the service and create the scope myself:

    return {
        showPopup: function () {
            var $scope = $rootScope.$new();
            $scope.data = {};

Still it feels awkward to have UI stuff within a service, so i'm open for alternatives.

Stephan

Sign up to request clarification or add additional context in comments.

Comments

0

I think you are using popup in a wrong place. You must separate functionalities and showing a popup is a function that must be in controller instead.

If you do that way, it will work perfectly ;)

3 Comments

Why do you think popup is a function that must be in a controller?
Because I think that services must provide data to controllers, and controllers must use this data to send it to view or, in this case, make changes in the view, like change the view or load popup windows. It is a good practice to separate functionalities and MVC patterns are for this purpose.
Don't you think all popups that are in application should be called from one global method in app that will differentiate which type of popup should be called and according to it will call it, rather implementing this in each controller where i need to call popup. Is that global method has to be placed in a new service ?

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.