Access scope inside an angular js factory

10,892

In fact, you can access to the scope in your factory. The reason you cannot is there's no such a variable called $scope in your parentGate.show function.

Seems you want to use a factory to pop up a dialog.

I think in your case, you will pass you scope as a parameter when u try to invoke

angular.module("yourApp").controller("testController", function($scope, parentGate){
    parentGate.show($scope, callback);
});

And in your factory, when you try to change the property value under $scope, (onTap callback) you should use scope, not $scope

onTap: function(e) {
    //if ($scope.parentGate.answer == first + second) { 
    if (scope.parentGate.answer == first + second) { 
        console.log("correct");
        SuccessCallback();
     } else {
         console.log("wrong!");
         e.preventDefault();
     }
 }

Here is the demo code.
Here is the reason why we want to change $scope to scope in your onTap callback (Demo Closure)

Hope this will work. : )

Share:
10,892
Dev01
Author by

Dev01

Updated on June 25, 2022

Comments

  • Dev01
    Dev01 almost 2 years

    I'm using the ionic framework and need to be able to call a popup from muliple places in my code so I thought I would move it into a factory. The popup uses an input field and I want to get the value of it. Normally I would just call $scope.parentGate.answer but because it's in a factory I don't have access to the scope. Any ideas how I can get the value of the input field?

    Here's my code:

    angular.module('starter.services', [])
    .factory('parentGate', function ($ionicPopup, Helpers) {
        return {
            Show: function(scope, SuccessCallback) {
                var first = Helpers.RandomNumber(1, 5) * 10;
                var second = Helpers.RandomNumber(11, 22);
                var title = 'What does ' + first + ' + ' + second + ' equal?'
                // An elaborate, custom popup
                return $ionicPopup.show({
                    template: '<h4 class="text-center">' + title + '</h4><div class="list"><label class="item item-input"><input type="number" ng-model="parentGate.answer" placeholder="Answer..."></label></div>',
                    title: "Parent Gate",
                    //subTitle: title,
                    scope: scope,
                    buttons: [
                      { text: 'Cancel' },
                      {
                        text: 'Continue',
                        type: 'button-positive',
                        onTap: function(e) {
    
                          //
                          // I can't access $scope.parentGate.answer here.  
                          // How else can I do it?
                          //
                          if ($scope.parentGate.answer == first + second) { 
    
                            console.log("correct");
                            SuccessCallback();
                          } else {
                            console.log("wrong!");
                            e.preventDefault();
                          }
                        }
                      }
                    ]
                });
            }
        };
    });
    
  • Dev01
    Dev01 almost 9 years
    I was close. :) Thanks for help.