Volta 在 ng-conf 中谈到的新依赖注入(inject),包含在此处:https://github.com/angular/di.js正是我正在寻找的 AngularJS 1.3.0 项目。
问题是,我不清楚我是否可以使用它。在 github 项目示例中似乎没有将它用于 AngularJS v1 的示例。
我遇到了一个在 Backbone 项目中使用它的示例:http://teropa.info/blog/2014/03/18/using-angular-2-0-dependency-injection-in-a-backbone-app.html我发现了一个在 AngularJS v1 项目中使用 ES6 的示例:https://github.com/mvolkmann/todo-es6/ ,但我找不到在 Angular v1 项目中使用新 DI 的示例。
我很困惑。任何指针?
最佳答案
也许不使用 di.js,而是将类似样式的代码转换为有效的 Angular 1.X 语法(在构建步骤期间)
一个小例子和一个可能的开始:
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);
也许您甚至可以使用某些属性(例如
@NgController
等)在您的模块上将其注册为 Controller 。
关于angularjs - 我可以在 AngulrJS 1.3.0 项目中使用 angular/di.js 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25974730/