考虑以下:
HTML
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js"></script>
<meta charset=utf-8 />
<title>Exploring directives</title>
</head>
<body>
<my-directive>
Some text
</my-directive>
{{Info.Version}}
<script type="text/ng-template" id='my-directive.html'>
<div>Hello, {{Info.Version}}</div>
</script>
</body>
</html>
JS
var app = angular.module('myApp', []);
app.run(function($rootScope) {
$rootScope.Info = {};
$rootScope.Info.Name = 'rootScope';
$rootScope.Info.Version = '1.0.0.1';
});
app.directive('myDirective', function() {
return {
restrict: 'E',
templateUrl: 'my-directive.html',
scope: false
};
});
巴达朗
据我所见, $rootScope 从未显示(但我猜如果是的话,它会被标记为Scope (001))。
当我在JS中将范围更改为 true 时:
app.directive('myDirective', function() {
return {
restrict: 'E',
templateUrl: 'my-directive.html',
scope: true
};
});
似乎$ rootScope被下推到Scope (002):
有人可以解释发生了什么吗?为什么 $rootScope 似乎被推倒了?那上面是什么?这是 jsbin 链接:http: //jsbin.com/udALigA/1/