如何在 angularjs 指令中的链接函数内访问函数中的范围

How to access scope in a function inside link function in angularjs directive?

本文关键字:函数 访问 范围 链接 指令 angularjs      更新时间:2023-09-26

这可能不是一个角度JS问题,但我在这里已经到了尽头。代码如下所示:

prep.directive('resultgraph', function () {
  return {
    restrict: 'A',
    link: function (scope, element, attrs) {
        //** scope accessible here **
        DomReady.ready(function () {
            ThreeBox.preload([
                '/scripts/lib/snippets.glsl.html',
            ], function () {
               //....scope not accessible here

如何访问"preload"回调函数中的范围,其中说此处无法访问范围?

如果你需要 DOM 准备好,你可以在链接函数中执行此操作(范围将可访问):

$timeout(function(){
    alert('DOM ready');
    //** scope accessible here **
});