我正在参考一个AngularJS的学习资料,并尝试实现“具有隔离作用域的指令”,但它不起作用。请查看下面给出的代码片段,我已经完成了。
HTML:
Angular JS:
实际输出:
预期输出:
“输出的最后一行有所不同。”
“我同意,在SO上已经有几个标题相似的问题。我已经看过了它们,相信我,没有一个能够回答我的疑问。因此,考虑提出符合我的要求的问题。”
HTML:
<body ng-app="myApp">
<div ng-init="myProperty = 'wow, this is cool'">
Surrounding scope: {{ myProperty }}
<div my-inherit-scope-directive>
Inside an directive with inherited scope: {{ myProperty }}
</div>
<div my-directive>
Inside myDirective, isolate scope: {{ myProperty }}
<div>
</div>
</body>
JS:
var app = angular.module('myApp', []);
app.directive('myDirective', function() {
return {
restrict: 'A',
scope: {}
};
});
app.directive('myInheritScopeDirective', function() {
return {
restrict: 'A',
scope: true
};
});
Angular JS:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.js"></script>
实际输出:
Surrounding scope: wow, this is cool
Inside an directive with inherited scope: wow, this is cool
Inside myDirective, isolate scope: wow, this is cool
预期输出:
Surrounding scope: wow, this is cool
Inside an directive with inherited scope: wow, this is cool
Inside myDirective, isolate scope:
“输出的最后一行有所不同。”
“我同意,在SO上已经有几个标题相似的问题。我已经看过了它们,相信我,没有一个能够回答我的疑问。因此,考虑提出符合我的要求的问题。”
1.2.0-rc.3
,而这是在引入这些更改之前的1.2.0
版本之前的版本 :) 已更新答案。 - tanmay