gpt4 book ai didi

javascript - 嵌入如何与隔离范围一起工作

转载 作者:行者123 更新时间:2023-12-03 11:44:32 24 4
gpt4 key购买 nike

我写了这段代码。

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.hobby='AngularJS';
});

app.directive('outputText',function(){
return {
transclude: true,
scope: {},
template: '<div ng-transclude></div>'
}
});
<!DOCTYPE html>
<html ng-app="plunker">

<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.2.x" src="http://code.angularjs.org/1.2.7/angular.js" data-semver="1.2.7"></script>
<script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
<div output-text>
<p>Hello {{name}}</p>
</div>
<div output-text>
<p>I love {{hobby}}!!</p>
</div>
</body>

</html>

正如您在上面看到的,我在指令中使用隔离范围,其中隔离范围中没有名称属性。但是它如何访问父作用域名称属性,因为我什至没有使用子作用域或者我丢失了一些东西。

最佳答案

无论您是否使用隔离,嵌入的元素始终绑定(bind)到父范围。

这就是documentation on transclude

transclude

compile the content of the element and make it available tothe directive. Typically used with ngTransclude. The advantage oftransclusion is that the linking function receives a transclusionfunction which is pre-bound to the correct scope. In a typical setupthe widget creates an isolate scope, but the transclusion is not achild, but a sibling of the isolate scope. This makes it possible forthe widget to have private state, and the transclusion to be bound tothe parent (pre-isolate) scope.

关于javascript - 嵌入如何与隔离范围一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26114433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com