【转】简单模拟angular的依赖注入
原文: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的依赖注入的更多相关文章
- 用原生js简单模仿angular的依赖注入
大家都知道angular 依赖注入很神奇,跟我们平常写代码的风格思维差别很大,不过仔细分析确是一个很有意思的东西,依赖注入早期也叫依赖倒置,是java中有的.废话不多少直接上例子 本帖属于原创,转载请 ...
- 模拟AngularJS之依赖注入
一.概述 AngularJS有一经典之处就是依赖注入,对于什么是依赖注入,熟悉spring的同学应该都非常了解了,但,对于前端而言,还是比较新颖的. 依赖注入,简而言之,就是解除硬编码,达到解偶的目的 ...
- angular 实现依赖注入
1:首先获取module对象var myAppModule = angular.module('myApp', []); 2:定义对象(类似spring中xml声明bean对象<bean id= ...
- Angular的依赖注入(依赖反转)原理说明
依赖注入(依赖反转)意思是由函数决定要引入什么样的依赖: let mod = angular.module('test',[]); mod.controller('test_c',function($ ...
- angular关于依赖注入
<html> <head> <title>Angular JS Forms</title> </head> <body> < ...
- Angular 4 依赖注入
一.依赖注入 1. 创建工程 ng new myangular 2. 创建组件 ng g componet product1 3. 创建服务 ng g service shared/product 如 ...
- angular 基本依赖注入
import { Injectable } from '@angular/core'; @Injectable() export class ProductServiceService { const ...
- Angular中依赖注入方式的几种写法
1.第一种写法 angular.module('App').controller('TestCtrl',['$scope', function($scope) {}]); 2.第二种写法 angula ...
- -_-#【Angular】依赖注入
AngularJS学习笔记 var BoxCtrl = function($scope, $element) { } var str = BoxCtrl.toString().replace(/\s/ ...
随机推荐
- callback in C
callback is nothing but passing the function pointer to the code from where you want your handler/ c ...
- mysql -- 备忘
select distinct(authorid),author from forum_post where tid=1;
- IOS tableViewCell单元格重用中的label重叠的问题
参考:http://zhidao.baidu.com/link?url=_oMUTo5SxUY6SBaxYLsIpN3i2sZ6SKG35MVlPJd2cNmUf9TGQFkKXX9EXwSwti0n ...
- __declspec(dllexport) 和 __declspec(dllimport)的作用
operatordll.h #include <iostream> #ifndef _WIN32 #define DLL_EXPORT#else #ifdef OPERATORDLL_EX ...
- 转 vi 技巧和诀窍:令人刮目相看的 10 个超酷命令
for ksh vi 编辑器的许多选项可以控制编辑会话的外观和感觉.使用 :set 命令修改 vi 中的会话设置.按 Escape 键进入命令模式之后,可以使用 :set all 命令显示选项和设置的 ...
- sed awk 小例
实现数据库批量更新与回滚 create database awktest; use awktest create table user( id int unsigned not null uni ...
- 在idea的maven项目使用el或jstl表达式
必须加上这句: <%@ page isELIgnored="false" %> 否则无法解析el或jstl表达式 <%@ taglib prefix=" ...
- 【同一直线最多点】 poj 1118+2606+2780
poj 1118 #include<iostream> using namespace std; #define N 700 struct point {int x,y;} pnt[N]; ...
- Goods transportation
Goods transportation time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- java 数据结构 队列的实现
java 数据结构队列的代码实现,可以简单的进行入队列和出队列的操作 /** * java数据结构之队列的实现 * 2016/4/27 **/ package cn.Link; import java ...