Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using node module in angularjs?

Tags:

node.js

What's the best practice for using external code, e.g. code found in node modules, in angular?

I'd like to use this https://www.npmjs.com/package/positionsizingcalculator node module in my angular app. I've created an angular service intended to wrap the node module, and now I want to make the service use the node module.

'use strict';

angular.module('angularcalculator')
  .service('MyService', function () {
    this.calculate = function () {
      return {
        //I want to call the node module here, whats the best practice?
      };
    }
  });
like image 382
fred Avatar asked Apr 17 '15 22:04

fred


People also ask

Can I use NodeJS with AngularJS?

Both can be combined to create isomorphic web applications. NodeJS is the cross-platform and a run-time environment for Javascript applications. AngularJS is an open-source platform for web application development which is maintained by Google. In order to use NodeJS, you need to install it into your system.

What is the use of node modules in Angular?

The node_modules directory is only for build tools. The package. json file in the app root defines what libraries will be installed into node_modules when you run npm install . Very often with an angular app, on your dev machine or on a build server, you use other Javascript libraries from npm (a node.

What is the use of module in AngularJS?

An AngularJS module defines an application. The module is a container for the different parts of an application. The module is a container for the application controllers. Controllers always belong to a module.


1 Answers

To do this, I would crack open the package and grab the .js out of it. This package is MIT license, so we can do whatever we want. If you navigate to /node_modules/positionsizingcalculator/ you'll find an index.js. Open that up and you'll see the moudle export, which takes a function that returns an object.

You'll notice this is an extremely similar pattern to .factory, which also takes a function that returns an object (or constuctor, depending on your pattern). So I'd do the following

.factory('positionsizingcalculator', function(){
        basicValidate = function (argument) {
        ... //Insert whole declaration in here
        return position;
}) 

and the inject it where you need it:

.controller('AppController', function(positionsizingcalculator){
    //use it here as you would in node after you inject it via require.
})

-- Edit: This is good for one off grabs of the JS, but if you want a more extensible solution, http://browserify.org/ is a better bet. It allows you to transform your requirements into a single package. Note that this could result in pulling down a lot more code that you might otherwise need, if you make one require bundle for your whole site, as this is not true AMD, and you need to to load everything you might want one the client, unless you make page specific bundles.

You'd still want to do the require in a factory and return it, to keep it in angular's dependency injection framework.

like image 199
Dylan Watt Avatar answered Oct 05 '22 11:10

Dylan Watt