Search code examples
javascriptjqueryangularjsmeteorangular-router

Load Controllers in Meteor JS


I'm new to Meteor. I implemented angularui: angular-ui-router but I have a problem, I tell you:

I have the following folders:

client

         -> main.js

         -> index.html

         -> login

                 -> login.js

                 -> login.html

         -> home

                 -> home.html

         -> compatibility

main.js contains the following:

import angularMeteor from 'angular-meteor';
import uiRouter from 'angular-ui-router';

angular.module('angularUPF', [
  angularMeteor,
  uiRouter
]);

angular.module('angularUPF').config(function($stateProvider, $urlRouterProvider) {

  $urlRouterProvider.otherwise("/");

  $stateProvider
    .state('home', {
      url: "/",
      templateUrl: "client/home/home.html"
    })
    .state('login', {
      url: "/login",
      templateUrl: "client/login/login.html",
      controller: "loginCtrl"
    });
});

The templates load me well, however the controller tells me that it does not exist,

loginCtrl.js

import angular from 'angular';
import angularMeteor from 'angular-meteor';
import uiRouter from 'angular-ui-router';

import template from './login.html';

angular.module('angularUPF', [])
    .controller('loginCtrl', function($scope) {
      'ngInject';

      $scope.firstName = "Anthony";
    $scope.lastName = "Camus";
    });

Errors:

Uncaught SyntaxError: Unexpected token <
    at eval (<anonymous>)
    at Function.globalEval (modules.js?hash=acbbea235a5e860a943adbe75f051d48f27c799a:56114)
    at text script (modules.js?hash=acbbea235a5e860a943adbe75f051d48f27c799a:64984)
    at ajaxConvert (modules.js?hash=acbbea235a5e860a943adbe75f051d48f27c799a:64019)
    at done (modules.js?hash=acbbea235a5e860a943adbe75f051d48f27c799a:64483)
    at XMLHttpRequest.<anonymous> (modules.js?hash=acbbea235a5e860a943adbe75f051d48f27c799a:64899)
    at Object.send (modules.js?hash=acbbea235a5e860a943adbe75f051d48f27c799a:64951)
    at Function.ajax (modules.js?hash=acbbea235a5e860a943adbe75f051d48f27c799a:64432)
    at Function.jQuery._evalUrl (modules.js?hash=acbbea235a5e860a943adbe75f051d48f27c799a:64595)
    at domManip (modules.js?hash=acbbea235a5e860a943adbe75f051d48f27c799a:61058)


Error: [ng:areq] Argument 'loginCtrl' is not a function, got undefined
http://errors.angularjs.org/1.4.8/ng/areq?p0=loginCtrl&p1=not%20aNaNunction%2C%20got%20undefined
    at http://localhost:3000/packages/angular_angular.js?hash=b4b9bf43d4c591e6e90c1fe259c40d8bb252d2e8:93:12
    at assertArg (http://localhost:3000/packages/angular_angular.js?hash=b4b9bf43d4c591e6e90c1fe259c40d8bb252d2e8:1840:11)
    at assertArgFn (http://localhost:3000/packages/angular_angular.js?hash=b4b9bf43d4c591e6e90c1fe259c40d8bb252d2e8:1850:3)
    at http://localhost:3000/packages/angular_angular.js?hash=b4b9bf43d4c591e6e90c1fe259c40d8bb252d2e8:9183:9
    at http://localhost:3000/packages/modules.js?hash=acbbea235a5e860a943adbe75f051d48f27c799a:44724:28
    at invokeLinkFn (http://localhost:3000/packages/angular_angular.js?hash=b4b9bf43d4c591e6e90c1fe259c40d8bb252d2e8:8866:9)
    at nodeLinkFn (http://localhost:3000/packages/angular_angular.js?hash=b4b9bf43d4c591e6e90c1fe259c40d8bb252d2e8:8360:11)
    at compositeLinkFn (http://localhost:3000/packages/angular_angular.js?hash=b4b9bf43d4c591e6e90c1fe259c40d8bb252d2e8:7756:13)
    at publicLinkFn (http://localhost:3000/packages/angular_angular.js?hash=b4b9bf43d4c591e6e90c1fe259c40d8bb252d2e8:7636:30)
    at updateView (http://localhost:3000/packages/modules.js?hash=acbbea235a5e860a943adbe75f051d48f27c799a:44643:23)

I know a little better AngularJS and I know that error appears when I do not load the JS file in the index.html. The problem is that in Meteor I tried to do the same thing and it did not work for me.

In summary: I need a way to load the JS files that represent the controllers.

I tried to add a line of code in the file server/main.js

import { Meteor } from 'meteor/meteor';
import '../imports/database/connect_db.js';
import '../imports/database/init_db.js';

Meteor.startup(() => {
  console.log('Server running!');
  $.getScript("../client/login/loginCtrl.js");
});

But it tells me that $ is not defined

My packages:

# Meteor packages used by this project, one per line.
# Check this file (and the other files in this directory) into your repository.
#
# 'meteor add' and 'meteor remove' will edit this file for you,
# but you can also edit it by hand.

meteor-base@1.1.0             # Packages every Meteor app needs to have
mobile-experience@1.0.5       # Packages for a great mobile UX
mongo@1.2.2                   # The database Meteor supports right now
reactive-var@1.0.11            # Reactive variable for tracker
tracker@1.1.3                 # Meteor's client-side reactive programming library

standard-minifier-css@1.3.5   # CSS minifier run for production mode
standard-minifier-js@2.1.2    # JS minifier run for production mode
shell-server@0.2.4            # Server-side component of the `meteor shell` command

insecure@1.0.7                # Allow all DB writes from clients (for prototyping)
twbs:bootstrap
jquery
fourseven:scss
angular
es5-shim
angularui:angular-ui-router
babel-compiler

Solution

  • It worked as follows:

    I modified the loginCtrl.js:

    Meteor.startup(function(){
      angular.module('angularUPF')
          .controller('loginCtrl', function($scope) {
            'ngInject';
    
            $scope.firstName = "John";
          $scope.lastName = "Doe";
          });
    })
    

    And the login.html was like this:

    <div ng-controller="loginCtrl">
         First Name: <input type="text" ng-model="firstName"><br>
         Last Name: <input type="text" ng-model="lastName"><br>
         <br>
         Full Name: {{firstName + " " + lastName}}
    </div>
    

    Is it really the right way?