js中没有反射的概念,那么angular是如何实现注入依赖的呢?
简单模拟一下~
var app = {
scope :
function () {
this.name =
'lala';
},
controller :
function (fn) {
str = fn.toString();
str = str.substring(str.indexOf(
'(' )+
1,str.indexOf(
')' )).replace(
/\s/g,
'');
var arr = str.split(
',');
fn.apply(
null, arr.map(
function (item) {
return new app[item]();
}));
}
}
app.controller(
function (scope ) {
console.log(scope.name);
})
}
转载请注明原文地址: https://ju.6miu.com/read-964657.html