原文:http://www.oschina.net/code/snippet_1181081_35136

代码片段

var angular = function(){};

Object.defineProperty(angular,"module",{
value:function(modulename,args){
var module = function(){
this.args = args;
this.factoryObject = {};
this.controllerObject = {};
}
module.prototype.factory = function(name,service){
//if service is not a function ...
//if service() the result is not a object ... and so on
this.factoryObject[name] = service();
}
module.prototype.controller = function(name,args){
var _self = this;
//init
var content = {
$scope:{},
scope:function(){
return content.$scope;
}
// $someOther:{...}
} var ctrl = args.pop();
console.log(typeof ctrl);
var factorys = [];
while(service = args.shift()){
if(service in content){
factorys.push(content[service])
}else{
factorys.push(_self.factoryObject[service])
} }
ctrl.apply(null,factorys); _self.controllerObject[name] = function(){
return content;
};
}
var m = new module();
window[modulename] = m;
return m;
}
})

  代码测试

var hello = angular.module('Test');

hello.factory("actionService",function(){
var say = function(){
console.log("hello")
}
return {
"say":say
}
}) hello.controller("doCtrl",['$scope',"actionService",function($scope,actionService){
$scope.do = function(){
actionService.say();
}
}]); //调用
hello.controllerObject.doCtrl().scope().do()

  

【转】简单模拟angular的依赖注入的更多相关文章

  1. 用原生js简单模仿angular的依赖注入

    大家都知道angular 依赖注入很神奇,跟我们平常写代码的风格思维差别很大,不过仔细分析确是一个很有意思的东西,依赖注入早期也叫依赖倒置,是java中有的.废话不多少直接上例子 本帖属于原创,转载请 ...

  2. 模拟AngularJS之依赖注入

    一.概述 AngularJS有一经典之处就是依赖注入,对于什么是依赖注入,熟悉spring的同学应该都非常了解了,但,对于前端而言,还是比较新颖的. 依赖注入,简而言之,就是解除硬编码,达到解偶的目的 ...

  3. angular 实现依赖注入

    1:首先获取module对象var myAppModule = angular.module('myApp', []); 2:定义对象(类似spring中xml声明bean对象<bean id= ...

  4. Angular的依赖注入(依赖反转)原理说明

    依赖注入(依赖反转)意思是由函数决定要引入什么样的依赖: let mod = angular.module('test',[]); mod.controller('test_c',function($ ...

  5. angular关于依赖注入

    <html> <head> <title>Angular JS Forms</title> </head> <body> < ...

  6. Angular 4 依赖注入

    一.依赖注入 1. 创建工程 ng new myangular 2. 创建组件 ng g componet product1 3. 创建服务 ng g service shared/product 如 ...

  7. angular 基本依赖注入

    import { Injectable } from '@angular/core'; @Injectable() export class ProductServiceService { const ...

  8. Angular中依赖注入方式的几种写法

    1.第一种写法 angular.module('App').controller('TestCtrl',['$scope', function($scope) {}]); 2.第二种写法 angula ...

  9. -_-#【Angular】依赖注入

    AngularJS学习笔记 var BoxCtrl = function($scope, $element) { } var str = BoxCtrl.toString().replace(/\s/ ...

随机推荐

  1. CentOS 7 BIND 搭建

    域名查找顺序 设置 /etc/host.conf 1. bind 安装 $ yum install bind bind-utilsnslookup (name server lookup) 在bind ...

  2. 编译 wl18xx驱动源码

    在做beagleboneblack移植的时候,wl18xx的驱动源码是自动编译的.但是移植到其他平台优越平台不一样,所以就不能自动编译 所以用其他方式编译.http://e2e.ti.com/supp ...

  3. Android sdk content loader

    方法一(关闭后重启): 遇到Eclipse右下角一直显示“Android sdk content loader 0%”的情况时,直接关掉Eclipse,有ADB进程在运行时通过进程管理器结束进程,然后 ...

  4. 扩展方法where方法查询不到数据,不会抛异常,也不是返回的null

    如题,“扩展方法where方法查询不到数据,不会抛异常,也不是返回的null”,示例代码如下: Product类: public class Product { private string name ...

  5. 1.1 sikuli 安装

    JRE7不支持sikuli,必须下载JRE6   更新号必须大于35 sikuli下载: http://www.cr173.com/soft/52775.html 或参照 http://www.cnb ...

  6. wchar_t * 与 char * 互相转换小记

    wchar_t * 与char *之间的转化只需要借助标准库里面的std::wcstombs和std::mbstowcs就能实现了

  7. iOS 开发之照片框架详解之二 —— PhotoKit 详解(下)

    本文链接:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-three.html 这里接着前文<iOS ...

  8. byte 读写文件

    import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import ja ...

  9. 二叉树,平衡树,红黑树,B~/B+树汇总

    二叉查找树(BST),平衡二叉查找树(AVL),红黑树(RBT),B~/B+树(B-tree).这四种树都具备下面几个优势: (1) 都是动态结构.在删除,插入操作的时候,都不需要彻底重建原始的索引树 ...

  10. Codeforces 691B s-palindrome

    水题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #includ ...