原文: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. 关于stringWithFormat: - 两段NSString如何合成一段

    http://blog.sina.com.cn/s/blog_6b1e4a0601019pib.html str = [NSString stringWithFormat:@"%@,%@&q ...

  2. jax-rs的客户端完整实例

    本地接口: @Override    public Response formsubGet(String accountContent, char inputContent,            S ...

  3. 使用PLSQL Developer连接Oracle Database 11g Express Edition

    要使用oracle数据库,需要准备三部分: 1.oracle服务端 2.oracle客户端 3.连接工具 你装的Oracle Database 11g Express Edition就是服务端,pls ...

  4. php获取当前文件绝对路径

    php如何获取当前文件的绝对路径. dirname(__FILE__) 函数返回的是脚本所在在的路径 <?php $basedir = dirname(__FILE__); echo $base ...

  5. Notification使用笔记

    之前在项目中使用了Notification,现分享出来: checkNotification() function checkNotification(){ //判断是否支持Notification ...

  6. 【二分图】 poj 1466

    #include <iostream> #include <memory.h> #include <cstdio> using namespace std; int ...

  7. HTML day03表格与表单

    1.表格 一般格式: <table> <thead><!--表格头--> <tr> <th></th> </tr>& ...

  8. zTree异步加载并初始化树时全部展开(贴主要代码)

    <%@page pageEncoding="UTF-8"%> <%@include file="/commons/include/html_doctyp ...

  9. Net Protocol Related

    Data used to deliver through net should be encapsulated. General encapsulation include 4 layer of he ...

  10. Learning Java IO indexes

    I/O Streams, it simplifies I/O operations, write a whole object out to stream & read back. File ...