run function of a scope in other scope angularjs

Below is a working code running createAdmobBanner function. But how can I run the createAdmobBanner function in other 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.

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

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

    });
});

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?