Created
July 25, 2016 09:41
-
-
Save iplus26/524a41ad86d22cfbac475d86bf591fa9 to your computer and use it in GitHub Desktop.
Angular 中插入依赖的几种方式
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content=""> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body ng-app="mod"> | |
<div directive-one></div> | |
<div directive-two></div> | |
<div directive-three></div> | |
<div directive-four></div> | |
<script> | |
var mod = angular.module('mod', []) | |
.service('con', function() { | |
this.log = function() { return 'apple'; }; | |
}) | |
// 第一种写法, 可能会因为变量重命名被 minifier 干掉 | |
.directive('directiveOne', function(con) { | |
return { | |
template: con.log() | |
} | |
}) | |
// 第二种写法, 比较冗长 | |
.directive('directiveTwo', ['con', function(con) { | |
return { | |
template: con.log() | |
} | |
}]) | |
// 第三种写法 | |
var directiveThree = function(con) { | |
return { | |
template: con.log(), | |
} | |
} | |
// 不写这一行的话就和第一种写法一模一样 | |
directiveThree.$inject = ['con']; | |
mod.directive('directiveThree', directiveThree); | |
// 第四种写法 | |
var directiveFour = function(con) { | |
return { | |
template: con.log() | |
} | |
} | |
// 这一行会报错 | |
directiveFour.$inject = []; | |
mod.directive('directiveFour', directiveFour); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment