1

How can I run the working code in createAdmobBanner function in another controller?

angular.module('starter', ['ionic', 'starter.controllers'])

.run(function ($ionicPlatform) {
    $ionicPlatform.ready(function () {

        var admobid = {};
        if (/(android)/i.test(navigator.userAgent)) {
            admobid = {
                banner: 'ca-app-pub-3815248714018431/123456789'
            };
        }

        function createAdmobBanner() {
            AdMob.createBanner({
                adId: admobid.banner
                adSize: 'SMART_BANNER',
                position: 8
            });
        }

        createAdmobBanner();

    });
})

I got createAdmobBanner is not defined if I simply do createAdmobBanner() in my controllers. I tried $rootScope but the plugin doesn't seem work with that.

3
  • function should be placed outside, there shouldn't be any wrapper to global function Commented Jun 30, 2015 at 14:38
  • 1
    @pankajparkar it should be wrap within $ionicPlatform.ready for the plugin to run. Commented Jun 30, 2015 at 14:49
  • Thats my bad..please add it as answer..I don;t have an idea about it..THanks :) Commented Jun 30, 2015 at 14:50

3 Answers 3

1

You need to add it into a service or attached in on $rootScope,

$rootScope solution - faster to implement but "dirty"

.run(function($ionicPlatform,$rootScope) { //add $rootScope dependency injection
$rootScope.createAdmobBanner = function(){
  AdMob.createBanner( { adId:admobid.banner
  adSize: 'SMART_BANNER',
  position:8 

  });
}
$rootScope.createAdmobBanner()

into your controllers, add the dependency $rootScope and call your function $rootScope.createAdmobBanner

Service Solution - cleaner & reusable

  1. Create a new service that has your function
  2. Inject your service into run
  3. call your service function into run
  4. inject your service into controllers
  5. call your service function into controllers
Sign up to request clarification or add additional context in comments.

7 Comments

I would suggest the service solution - if possible. Otherwise you end up polluting your rootScope and lose separation of concerns.
Agree too, the service solution is clearly the cleanest way to do and it and make code reusable.
I already said rootScope doesn't work with the plugin, I got error of ReferenceError: AdMob is not defined at Scope.$ionicPlatform.ready.$rootScope.createAdmobBanner
@AliceXu you are making it wrong if it does not work. Show is your try with rootScope. Have you seen mine ?
@AliceXu I don't think so. There is no impact of calling your function as you do actually and wrapping it into a $rootScope function; therefore, add more info on your $rootScope fucntion declaration if you expect a correct answer.
|
0

I just found this link here. Give it a try. The important code looks like this:

var admobApp = angular.module('myapp', ['ionic'])
    .run(function($ionicPlatform, $ionicPopup) {
        $ionicPlatform.ready(function() {
            if(window.plugins && window.plugins.AdMob) {
                var admob_key = device.platform == "Android" ? "ANDROID_PUBLISHER_KEY" : "IOS_PUBLISHER_KEY";
                var admob = window.plugins.AdMob;
                admob.createBannerView( 
                    {
                        'publisherId': admob_key,
                        'adSize': admob.AD_SIZE.BANNER,
                        'bannerAtTop': false
                    }, 
                    function() {
                        admob.requestAd(
                            { 'isTesting': false }, 
                            function() {
                                admob.showAd(true);
                            }, 
                            function() { console.log('failed to request ad'); }
                        );
                    }, 
                    function() { console.log('failed to create banner view'); }
                );
            }
        });
    });

The admob stuff is within $ionicPlatform.ready(function() { and is defined like this var admob = window.plugins.AdMob;

Does that help?

5 Comments

Disclaimer: I never worked with Ionic or admob. I just read the code on the linked website and thought it is worth sharing.
how does this help my question? I want to call it within my other controller.
You wrote that you get a "ReferenceError: AdMob is not defined" using the rootScope approach. I thought that perhaps the problem is not the injection or rootScope stuff itself but that AdMob is just not defined at the right place. As aorfevre already said: It should work with the two approaches he mentioned...
hmm without using rootScope it worked too, if I call the function within the $ionicPlatform.ready scope.
Where do you define "AdMob"? And just for debugging - perhaps you want to save AdMob at the time it is valid and defined in the rootScope and then use $rootScope.AdMob... A plunkr is not possible, I guess? ;)
0

Try to define external angular service/factory and provide this service to any controller you need using dependency injection. This is a good practice to share common logic or data in this way.

EDIT:

angular.module('starter', ['ionic', 'starter.controllers']);

angular.module('starter').factory('bannerFactory',function(){
    return {
        createAdmobBanner: function(){
            window.plugins.AdMob.createBanner({ adId:admobid.banner
                                  adSize: 'SMART_BANNER',
                                  position:8 
                                });
        }
    }
});

angular.module('starter').controller('anyController',['bannerFactory', function(bannerFactory){
    bannerFactory.createAdmobBanner();
}]);

angular.module('starter').run(function ($ionicPlatform,bannerFactory) {
    $ionicPlatform.ready(function () {

        bannerFactory.createAdmobBanner();

    });
});

7 Comments

try to inject 'bannerfactory' to a function within run like this: .run(function($ionicPlatform,$rootScope, bannerfactory)
[$injector:modulerr] Failed to instantiate module starter due to: Error: [$injector:nomod] Module 'starter' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
probably you trying to define something to angular module before the module was defined. I modified my example above, please check if you can see something that you might doing wrong?
make sure the plugin js file included into your index.html file and it should be included before your angular files.
|

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.