Angularjs 指令模板中的作用域生成不执行';t与transclude一起工作
如何在Angularjs中定义一个指令,该指令获取参数并使用ngIf或ngRepeat排除子元素 可以在这里找到问题的完整演示-。以下是HTML:Angularjs 指令模板中的作用域生成不执行';t与transclude一起工作,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,如何在Angularjs中定义一个指令,该指令获取参数并使用ngIf或ngRepeat排除子元素 可以在这里找到问题的完整演示-。以下是HTML: <div ng-controller='myController'> <my-directive condition="flag"> Both 'flag' and 'a' are defined on controller's scope. Hi. {{a}} -> Nothin
<div ng-controller='myController'>
<my-directive condition="flag">
Both 'flag' and 'a' are defined on controller's scope.
Hi. {{a}} -> Nothing is shown after 'Hi'.
</my-directive>
</div>
现在,指令内的转包元素无法访问控制器的作用域。如何缓解这种情况
作为参考,Angular project上有这样一个问题-。我为此找到了一个解决方法 通过将
setTransscope
注入指令并在链接阶段调用它,正确的范围被标记为转置。不应在模板中使用ng transclude
指令,而应使用ng transscope
指令,以恢复先前标记的范围
指令
myApp.directive('myDirective', function(setTransscope) {
return {
scope: {
display: "="
},
transclude: true, // required
restrict: 'EA',
templateUrl: 'my-template.html',
link: setTransscope
/* ... or call within your own link function:
link: function($scope, $element) {
setTransscope($scope, $element);
// ...
// Other stuff
// ...
} */
};
});
<div ng-if="display">
<div ng-transscope></div>
</div>
模板
myApp.directive('myDirective', function(setTransscope) {
return {
scope: {
display: "="
},
transclude: true, // required
restrict: 'EA',
templateUrl: 'my-template.html',
link: setTransscope
/* ... or call within your own link function:
link: function($scope, $element) {
setTransscope($scope, $element);
// ...
// Other stuff
// ...
} */
};
});
<div ng-if="display">
<div ng-transscope></div>
</div>
非常感谢。测试-。我只在调用
$element.empty()时遇到问题代码>。由于某种原因,空的
被低估了。没有它,指令显然起了作用。范围转储程序似乎是一个功能强大且简单的调试工具。