如何延迟子指令的编译阶段,因为父指令的范围具有它所需的所有数据
how to defer compilation phase of the child directives unti the scope of parent directive has all data it requires
我有以下html结构:
<my-parent-directive>
<my-child-directive some-options="options">
以及应该实例化$scope
的myParentDirective
的控制器:
ng.module("some").directive("myParentDirective", ["someService", function(someService) {
return {
controller: function($scope) {
someService.getData().then(function(options) {
$scope.options = options;
});
}
}
});
由于getData
方法是异步的,并且可能需要一段时间才能返回对子指令至关重要的选项,因此我需要暂停子指令的编译''链接阶段,直到options
到达。我该怎么做?
是的,实际上很有可能在收到数据后手动将子代码插入父指令中。下面是一个简短的示例:
app.directive('myParentDirective', ["someService", function(someService) {
return {
restrict: 'E',
transclude: true,
link: function($scope, element, attr, controller, transclude) {
someService.getData().then(function(options) {
$scope.options = options;
// Grab the inner content of the directive
var innerContent = transclude($scope, function() {});
// And place it inside & recompile it
element.html(innerContent);
// For angular version 1.2.17 or lesser, compile the content
$compile(element.contents())($scope);
});
}
};
}]);
相关文章:
- AngularJS指令范围约束问题重复出现
- AngularJS:从控制器访问特定的指令范围
- 隔离范围-仅在指令范围内定义的值必须执行更改
- 指令范围
- 在ng重复中使用时的指令范围
- 角度指令模板输入不会更新指令范围
- 属性内的角度指令范围继承
- AngularJS:如何在指令范围的对象发生变化时更新与控制器作用域相关联的控制器作用域
- 如何从父指令/控制器访问指令范围
- AngularJS:在成功的HTTP请求后更新指令范围
- 购物指令范围问题
- 角度指令范围为空
- 在指令“范围”中需要值属性
- AngularJS 指令范围和$compile - 返回空白的变量
- AngularJS指令范围似乎只接受一个参数
- AngularJS :如何从相同的指令控制器访问指令范围
- 通过指令范围传递对象不起作用
- 指令范围重写父值
- 角度指令 - 范围返回 0 而不是值
- AngularJS 父指令范围来自内部 ng-transclude