加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 可以在AngulrJS 1.3.0项目中使用angular / di.js吗

发布时间:2020-12-17 07:38:09 所属栏目:安全 来源:网络整理
导读:Volta在ng-conf上谈到的新的依赖注入,包含在这里: https://github.com/angular/di.js正是我正在为我的AngularJS 1.3.0项目寻找的. 问题是,我不清楚我是否可以使用它.在github项目示例中,似乎没有使用AngularJS v1的例子. 我在Backbone项目中发现了一个例子
Volta在ng-conf上谈到的新的依赖注入,包含在这里: 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,而是将类似样式的代码转换成有效的角度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等)将其注册到您的模块上作为控制器.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读