0
votes

I've been trying to create a service to share code between two different components in Meteor 1.3, but have been spectacularly unsuccessful so far. Creating and injecting Angular 1 services just doesn't seem to work.

I have an Angular service like so:

angular.module(name).service("myService", function () {
    this.somevariable = 'somevalue';
});

I have a login component like so:

class Login {
    constructor($scope, $reactive, $state, myService) {
        console.log(myService.somevariable) //doesn't work
    }
}

// create a module
export default angular.module(name, [
    angularMeteor
]).component(name, {
    templateUrl: 'imports/ui/components/${name}/${name}.html',
    controllerAs: name,
    controller: Login
});

I just cant seem to be able to get the service injected in the component.What am I doing wrong?

SOLUTION:

I needed a service to validate an email address using a regular expression. I did it like this:

import angular from 'angular';
import angularMeteor from 'angular-meteor';
class Validator { 
    validateEmail(email) {
        var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        return re.test(email);
    }
}

const name = 'validator';

// create a module
export default angular.module(name, [
    angularMeteor
])

.service("Validator", Validator);

I then injected the service like so:

import {name as Validator} from '../../../api/services/validator'

class Login {
    constructor($scope, $reactive, $state, Validator) {
        'ngInject';
        this.$state = $state;

        $reactive(this).attach($scope);
        this.Validator = Validator;
    }

    login() {
        if(this.Validator.validateEmail(this.credentials.email)) {
            // email is valid. 
        }
    }    
}

const name = 'login';

export default angular.module(name, [
    angularMeteor,
    Validator
]).component(name, {
    templateUrl: `imports/ui/components/${name}/${name}.html`,
    controllerAs: name,
    controller:Login
})

Hope this helps :)

1
Are $scope, $reactive and $state correctly injected? Have you shared all your code here? The code to inject is missing in your sample.GG.
$state, $reactive and $scope are working perfectly. I want to inject the UserService and use its method getStuff().lostsoul29

1 Answers

0
votes

I was able to set up a service with angular and meteor 1.3 by doing what you did but also adding "ngInject"; to the constructor. This is what it would look like

class Login {
    constructor($scope, $reactive, $state, myService) {
        "ngInject"; //this is needed to inject
        console.log(userService.getStuff());
    }
}

// create a module
export default angular.module(name, [
    angularMeteor
]).component(name, {
    templateUrl: 'imports/ui/components/${name}/${name}.html',
    controllerAs: name,
    controller: Login
}).service("userService", function () {
    this.getStuff = function() {
        return "Got Stuff";
    };
});;