1、问题背景
AngularJS特有的一个依赖注入,当在一个对象创建时,需要依赖另一个对象。
2、实现源码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>AngularJS之依赖注入(实例一)</title>
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<style>
body,html{
font-family: "微软雅黑";
font-size: 12px;
font-feature-settings: on;
}
.showContent{
200px;
padding: 10px;
margin: 10px;
border: 1px dashed #2AABD2;
}
</style>
<script>
var app = angular.module("reApp",[]);
app.config(function($controllerProvider){
$controllerProvider.register("reCon",["$scope",function($scope){
$scope.content = "";
$scope.clickShow = function(){
$scope.content = "showContent";
$scope.showContent = "依赖注入";
};
}]);
});
</script>
</head>
<body ng-app="reApp">
<div ng-controller="reCon">
<button type="button" ng-click="clickShow();">依赖注入</button><br><br>
<label class="{{content}}">{{showContent}}</label>
</div>
</body>
</html>
3、实现结果
(1)初始化
(2)点击按钮时