bal*_*teo 8 angularjs angularjs-directive angularjs-scope batarang
我有一个关于AngularJs范围的问题,特别是有关使用Batarang Chrome扩展程序进行检查的方式.
我有以下html:
<!doctype html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="utf-8">
<title>My AngularJS App</title>
<link rel="stylesheet" href="css/app.css"/>
</head>
<body>
<div ng-controller="myCtrl">
<div enhanced-textarea ng-model="name"></div>
<div cmp>
<h3>{{name}}</h3>
<div notice></div>
</div>
</div>
<script src="lib/angular/angular.js"></script>
<script src="js/directives.js"></script>
<script src="js/controllers.js"></script>
<script src="js/app.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
以下是指令:
'use strict';
angular.module('myApp.directives', [])
.directive('cmp', function () {
return {
restrict: 'A',
controller: 'cmpCtrl',
replace: true,
transclude: true,
scope: {
name: '='
},
template: '<div ng-transclude></div>'
};
})
.controller('cmpCtrl', ['$scope', '$element', '$attrs' , function ($scope, $element, $attrs) {
$scope.$parent.$watch('name', function (newVal) {
if (newVal) {
$scope.$parent.updatedSize = newVal.length;
}
}, true);
}])
.directive('enhancedTextarea', function () {
return {
restrict: 'A',
replace: true,
transclude: true,
template: '<textarea ng-transclude></textarea>'
};
})
.directive('notice', function () {
return {
restrict: 'A',
require: '^cmp',
replace: true,
scope: {
updatedSize: '='
},
template: '<div>{{size}}</div>',
link: function ($scope, $element, $attrs, cmpCtrl) {
$scope.$parent.$watch('updatedSize', function (newVal) {
if (newVal) {
$scope.size = newVal;
}
}, true);
}
};
});
Run Code Online (Sandbox Code Playgroud)
和控制器:
'use strict';
angular.module('myApp.controllers', [])
.controller('myCtrl', ['$scope', function($scope) {
$scope.name = 'test';
}]);
Run Code Online (Sandbox Code Playgroud)
当我使用batarang检查范围时,我得出以下结论:
范围007:通知指令的链接功能
完整的应用程序位于github 这里
另请参见下面的屏幕截图:
并不是每个人$scope
都必须对应于您网页的元素.事实上,在每个AngularJS应用程序中$scopes
,都有一些不直接链接到任何元素.
在您的情况下,ng-transclude
它会导致创建子范围.
看一下AngularJS的实现,它会导致你的创建004
$scope
.
if (!transcludedScope) {
transcludedScope = scope.$new();
transcludedScope.$$transcluded = true;
scopeCreated = true;
}
Run Code Online (Sandbox Code Playgroud)
https://github.com/angular/angular.js/blob/master/src/ng/compile.js#L959
如果您想深入挖掘自己,请在AngularJS文件中设置断点:
然后只需使用调用堆栈并按照兔子...
归档时间: |
|
查看次数: |
4456 次 |
最近记录: |