Dev01
Dev01

Reputation: 14149

Access scope inside an angular js factory

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();
                      }
                    }
                  }
                ]
            });
        }
    };
});

Upvotes: 3

Views: 10917

Answers (1)

Tyler.z.yang
Tyler.z.yang

Reputation: 2450

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. : )

Upvotes: 11

Related Questions