Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use angular/di.js with an AngulrJS 1.3.0 project?

The new dependency injection that Volta spoke about at ng-conf and is contained here: https://github.com/angular/di.js is just what I am looking for for my AngularJS 1.3.0 project.

The problem is, it is not clear to me whether or not I can use it. There doesn't seem to be an example of using it for AngularJS v1 in the github project examples.

I came across an example using it in a Backbone project: http://teropa.info/blog/2014/03/18/using-angular-2-0-dependency-injection-in-a-backbone-app.html and I found an example of using ES6 in an AngularJS v1 project: https://github.com/mvolkmann/todo-es6/, but I can't find an example using the new DI in an Angular v1 project.

I'm confused. Any pointers?

like image 382
kpg Avatar asked Sep 22 '14 12:09

kpg


People also ask

What is the difference between Angular and AngularJS?

Angular JS, based on JavaScript, uses terms of scope and controllers while Angular uses a hierarchy of components. Angular is component-based while AngularJS uses directives.

Can I use node js with AngularJS?

If you are writing an AngularJS front end web application, you may never have to use NodeJS. If you need tooling (build scripts to compile Sass, linters, etc) in your development or deployment process you can use NodeJS task runners like Gulp, Grunt or Webpack.

Why is AngularJS called Angular?

Just googled: "why is angular js called angular". Because HTML has Angular brackets and "ng" sounds like "Angular". Show activity on this post. Because Angular JS can be written inside the angle brackets of HTML that is why it is known as Angular JS .


1 Answers

Maybe don't use di.js but instead transpile similarly styled code into valid angular 1.X syntax (during a build step)

A small example and a possible start:

var falafel = require('falafel');
var traceur = require('traceur');

var src =  
  '@Inject(MyService,MyOtherService)' +
  'class Thing{' +
  '  constructor(service,otherservice){' +
  '  }' +
  '}';

src = traceur.compile(src, { annotations: true });
//console.log(src);

function tryGetPath(obj, path) {
  path.split('.').forEach(function(key) {
    obj = obj && obj[key];
  });
  return obj;
}

var output = falafel(src, function(node) {
  //find `Object.defineProperty for 'annotations'`
  if (node.type === 'CallExpression' && tryGetPath(node, 'arguments.1.value') === 'annotations') {
    var injectable = tryGetPath(node, 'arguments.0.name');
    var $inject = (tryGetPath(node, 'arguments.2.properties.0.value.body.body.0.argument.elements') || [])
                       .filter(function(a){return a.callee.name === 'Inject'})  
                       .reduce(function(p,c){ p.push.apply(p,c.arguments); return p;},[])
                       .map(function(a){return "'"+a.name+"'";});
    node.update(injectable + '.$inject = [' + $inject.toString() + '];');
  }
});

console.log(output);

Perhaps you can even use certain attributes (eg @NgController etc) to register it on your module as a controller.

like image 111
calebboyd Avatar answered Sep 27 '22 19:09

calebboyd