Link 在 Angular js 中嵌入后的函数
Link function after transclude in Angular js
我在 Angular 中有两个 指令 。一个必须被排斥在另一个之中。我的问题是在 transclude
函数具有 运行 之后,我无法使用简单的 JQuery 选择器访问 DOM。特别是我想 编译第一个指令 (主题),然后 将它注入 在另一个指令(关于我们)中,这样我就可以 访问关于我们 link 功能中的 DOM 元素。
这是我想要实现的代码:
<section>
<topic active = "true" title = "Lorem ipsum" icon = "ti-layers">A) Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</topic>
<topic title = "Lorem ipsum" icon = "ti-package">B) Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</topic>
<topic title = "Lorem ipsum" icon = "ti-stats-up">C) Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</topic>
<topic title = "Lorem ipsum" icon = "ti-layout-media-center-alt">D) Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</topic>
</section>
关于我们和主题指令都在其配置中使用 transclude: true
来编译各自的模板。
angular.module('playground', []).directive('topic', function()
{
return {
restrict: 'E',
replace: true,
templateUrl: 'path/to/topic.html',
transclude: true,
scope: true,
link: function(scope, element, attributes, ctrl, transclude)
{
// Playing around with scope
// Transcluding
element.find('.tab-content p').append(transclude());
}
};
}).directive('section', ['$timeout', function($timeout)
{
return {
restrict: 'E',
replace: true,
templateUrl: 'path/to/about-us.html',
transclude: true,
link: function(scope, element, attributes, ctrl, transclude)
{
element.find('.tabs').append(transclude());
// Now I want to retrieve some DOM contents after the transclude has taken place
$timeout(function()
{
// Playing with DOM and JQuery but sometimes this function run prior of the child transclude function
});
}
};
}]);
为了完整起见,这里是两个模板的代码:
<!-- topic.html -->
<li class="ng-class:active">
<div class="tab-title">
<i class="icon ng-class:icon;"></i>
<span>{{title}}</span>
</div>
<div class="tab-content">
<!-- ng-transclude directive not needed -->
<p ng-transclude></p>
</div>
</li>
<!-- about-us.html -->
<section class="bg-secondary pb0" id="about-us">
<div class="container">
<div class="row">
<div class="col-sm-12 text-center">
<h2 class="mb64 mb-xs-24">About us.</h2>
</div>
<div class="col-md-8 col-md-offset-2 col-sm-12 text-center">
<div class="tabbed-content icon-tabs">
<ul class="tabs"></ul>
</div>
</div>
</div>
</div>
</section>
问题是 $timeout
作为 解决方法 调用的函数具有完全 随机行为 并且有时可以正常工作,其他时候则不然。我怎样才能解决这个问题?
我是否缺少有关 transclude、link 和 compile 函数的内容?
感谢您的每一个回复!
我已经解决了我的问题,在主指令上设置了一个简单的 watcher,并在 [=22] 中使用 Angular 注入的 transcludeFn
=] 函数。
这是代码。希望对遇到类似问题的你有所帮助
angular.module('playground', []).directive('topic', function()
{
return {
restrict: 'AE',
replace: true,
templateUrl: 'path/to/topic.html',
transclude: true,
scope: true,
link: function(scope, element, attributes, ctrl, transclude)
{
// Playing with scope...
transclude(scope.$parent, function(clone, parent_scope)
{
// Transcluding
element.find('.tab-content p').append(clone);
// It's time to render!
parent_scope.should_render = true;
});
}
};
}).directive('Section', function()
{
return {
restrict: 'E',
replace: true,
templateUrl: 'path/to/section.html',
transclude: true,
scope: {},
link: function(scope, element, attributes, ctrl, transclude)
{
scope.rendered = false;
scope.should_render = false;
transclude(scope, function(clone)
{
// Transcluding
element.find('.tabs').append(clone);
});
// Setting up a watcher
var remove_watcher = scope.$watch('should_render', function(should_render)
{
if(should_render)
{
if(scope.rendered)
{
remove_watcher();
return;
}
// Now I can play with the DOM and JQuery.
scope.rendered = true;
}
});
}
};
})
现在我可以像这样使用我的指令了:
<section>
<topic active = "true" title = "Lorem ipsum" icon = "ti-layers">A) Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</topic>
<topic title = "Lorem ipsum" icon = "ti-package">B) Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</topic>
<topic title = "Lorem ipsum" icon = "ti-stats-up">C) Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</topic>
<topic title = "Lorem ipsum" icon = "ti-layout-media-center-alt">D) Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</topic>
</section>
我在 Angular 中有两个 指令 。一个必须被排斥在另一个之中。我的问题是在 transclude
函数具有 运行 之后,我无法使用简单的 JQuery 选择器访问 DOM。特别是我想 编译第一个指令 (主题),然后 将它注入 在另一个指令(关于我们)中,这样我就可以 访问关于我们 link 功能中的 DOM 元素。
这是我想要实现的代码:
<section>
<topic active = "true" title = "Lorem ipsum" icon = "ti-layers">A) Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</topic>
<topic title = "Lorem ipsum" icon = "ti-package">B) Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</topic>
<topic title = "Lorem ipsum" icon = "ti-stats-up">C) Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</topic>
<topic title = "Lorem ipsum" icon = "ti-layout-media-center-alt">D) Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</topic>
</section>
关于我们和主题指令都在其配置中使用 transclude: true
来编译各自的模板。
angular.module('playground', []).directive('topic', function()
{
return {
restrict: 'E',
replace: true,
templateUrl: 'path/to/topic.html',
transclude: true,
scope: true,
link: function(scope, element, attributes, ctrl, transclude)
{
// Playing around with scope
// Transcluding
element.find('.tab-content p').append(transclude());
}
};
}).directive('section', ['$timeout', function($timeout)
{
return {
restrict: 'E',
replace: true,
templateUrl: 'path/to/about-us.html',
transclude: true,
link: function(scope, element, attributes, ctrl, transclude)
{
element.find('.tabs').append(transclude());
// Now I want to retrieve some DOM contents after the transclude has taken place
$timeout(function()
{
// Playing with DOM and JQuery but sometimes this function run prior of the child transclude function
});
}
};
}]);
为了完整起见,这里是两个模板的代码:
<!-- topic.html -->
<li class="ng-class:active">
<div class="tab-title">
<i class="icon ng-class:icon;"></i>
<span>{{title}}</span>
</div>
<div class="tab-content">
<!-- ng-transclude directive not needed -->
<p ng-transclude></p>
</div>
</li>
<!-- about-us.html -->
<section class="bg-secondary pb0" id="about-us">
<div class="container">
<div class="row">
<div class="col-sm-12 text-center">
<h2 class="mb64 mb-xs-24">About us.</h2>
</div>
<div class="col-md-8 col-md-offset-2 col-sm-12 text-center">
<div class="tabbed-content icon-tabs">
<ul class="tabs"></ul>
</div>
</div>
</div>
</div>
</section>
问题是 $timeout
作为 解决方法 调用的函数具有完全 随机行为 并且有时可以正常工作,其他时候则不然。我怎样才能解决这个问题?
我是否缺少有关 transclude、link 和 compile 函数的内容?
感谢您的每一个回复!
我已经解决了我的问题,在主指令上设置了一个简单的 watcher,并在 [=22] 中使用 Angular 注入的 transcludeFn
=] 函数。
这是代码。希望对遇到类似问题的你有所帮助
angular.module('playground', []).directive('topic', function()
{
return {
restrict: 'AE',
replace: true,
templateUrl: 'path/to/topic.html',
transclude: true,
scope: true,
link: function(scope, element, attributes, ctrl, transclude)
{
// Playing with scope...
transclude(scope.$parent, function(clone, parent_scope)
{
// Transcluding
element.find('.tab-content p').append(clone);
// It's time to render!
parent_scope.should_render = true;
});
}
};
}).directive('Section', function()
{
return {
restrict: 'E',
replace: true,
templateUrl: 'path/to/section.html',
transclude: true,
scope: {},
link: function(scope, element, attributes, ctrl, transclude)
{
scope.rendered = false;
scope.should_render = false;
transclude(scope, function(clone)
{
// Transcluding
element.find('.tabs').append(clone);
});
// Setting up a watcher
var remove_watcher = scope.$watch('should_render', function(should_render)
{
if(should_render)
{
if(scope.rendered)
{
remove_watcher();
return;
}
// Now I can play with the DOM and JQuery.
scope.rendered = true;
}
});
}
};
})
现在我可以像这样使用我的指令了:
<section>
<topic active = "true" title = "Lorem ipsum" icon = "ti-layers">A) Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</topic>
<topic title = "Lorem ipsum" icon = "ti-package">B) Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</topic>
<topic title = "Lorem ipsum" icon = "ti-stats-up">C) Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</topic>
<topic title = "Lorem ipsum" icon = "ti-layout-media-center-alt">D) Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</topic>
</section>