mrcdnk
mrcdnk

Reputation: 95

Correct way to access emojione in angular controller

I have a PhoneGap App with AngularJs 1.5.5, OnsenUI 2 and EmojiOne.

I am trying to access the emojione variable inside of the angular controller:

ons.bootstrap.controller('AppController', ['$scope', '$timeout','$http', '$sce', function ($scope, $timeout, $http, $sce) {

    emojione.imageType = 'svg';
    emojione.sprites = true;
    emojione.imagePathSVGSprites = '../res/sprites/emojione.sprites.svg';
    ...

I have some function calls later on as well.

It is working as intended when i run it on my PC, but fails to resolve "emojione" (it is undefined) on android. This is my first time working with angularjs and JavaScript so it may be something really simple.

Upvotes: 1

Views: 458

Answers (2)

Breixo
Breixo

Reputation: 3100

Are you loading emojione library from a cdn? Mobile has internet access?

Upvotes: 1

DavidA
DavidA

Reputation: 4184

When dealing with angularjs and standard javascript libraries, I like to make a module and factory simply for accessing the root variable, just like you are trying to do:

angular.module('emojioneModule', [])
.factory('emojione', function($window) {
    return $window.emojione;
});

This allows you to inject the reference, and allows you to mock up the emojione for unit tests, as you can inject a mock instead.

ons.bootstrap.controller('AppController', ['$scope', '$timeout','$http', '$sce', 'emojione', function ($scope, $timeout, $http, $sce, emojione) {

    emojione.imageType = 'svg';
    emojione.sprites = true;
    emojione.imagePathSVGSprites = '../res/sprites/emojione.sprites.svg';
    ...

However, you could just inject $window, and access it directly from there.

Upvotes: 1

Related Questions