【问题标题】:AngularJS 1.5 config delayAngularJS 1.5 配置延迟
【发布时间】:2016-07-18 06:12:15
【问题描述】:

我正在学习 AngularJS 和 TypeScript,并构建了一个非常基本的应用程序来让自己继续前进。

一个将元素指令加载到页面上的应用程序,非常简单,但是在尝试加载指令挂钩时,我一直遇到错误:“Uncaught TypeError: Cannot read property 'directive' of null”。

我明白为什么会发生这种情况,但我在互联网上找不到任何有关如何解决此问题的信息。

TS 供参考:

app.ts

namespace playground.core
{
    "use strict";

    export let playgroundApp: ng.IModule = angular.module("playgroundApp", []);
    export let compileProvider: ng.ICompileProvider = null;

    playgroundApp.config(($compileProvider: ng.ICompileProvider, $controllerProvider: ng.IControllerProvider) => {
        (<any>$controllerProvider).allowGlobals();
        compileProvider = $compileProvider;
    }); 

    angular.element(document).ready(() => { angular.bootstrap(document, ["playgroundApp"]); });
}

PlaygroundController.ts

namespace playground.controllers
{
    "use strict";
    export interface IPlaygroundScope extends ng.IScope
    {

    }

    export class PlaygroundController
    {
        static $inject: string[] = ["$scope", "$element"];

        private scope: IPlaygroundScope;

        constructor($scope: IPlaygroundScope, $element: ng.IRootElementService)
        {
            this.scope = $scope;
        }
    }
}

directive.ts

namespace playground.directives
{
    "use strict";

    export interface ILoaderScope extends ng.IScope
    {

    }

    export class LoaderDirective
    {
        private scope: ILoaderScope;

        constructor($scope: ILoaderScope, $rootElement: ng.IRootElementService, $attributes: ng.IAttributes, $compile: ng.ICompileService)
        {
            this.scope = $scope;
        }
    }

    playground.core.compileProvider.directive.apply(null, ["loader", ["$compile", ($compile: ng.ICompileService) =>
    {
        return {
            restrict: "E",
            replace: true,
            templateUrl: "template.html",
            scope: {

            },
            link: ($scope: ILoaderScope, $rootElement: ng.IRootElementService, $attributes: ng.IAttributes) =>
            {
                return new LoaderDirective($scope, $rootElement, $attributes, $compile);
            }
        };
    }]]);
}

index.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <link href="App_Themes/Designer/designer.css" rel="stylesheet" />
</head>
<body data-ng-controller="playground.controllers.PlaygroundController">

    <div k2-loader=""></div>

    <script src="scripts/jquery.min.js"></script>
    <script src="scripts/angular.min.js"></script>    
    <script src="scripts/go-debug.js"></script>

    <script src="core/app.js"></script>
    <script src="controllers/PlaygroundController.js"></script>
    <script src="directives/loader/directive.js"></script>

    <script type="text/javascript">

        "use strict";        

    </script>
</body>
</html>

directive.ts 中抛出异常:playground.core.compileProvider.directive.apply... 因为在 playgroundApp.config(...) 上似乎有延迟 仅在指令已加载后调用。

有没有办法让这个场景工作,而不必将指令调用包装在 setTimeout(...) 中?

更新:

所以我最终将指令部分包装在检查中,并且无论如何都使用 setTimeout,因为似乎没有其他解决方案。

namespace playground.directives
{
    "use strict";

    export interface ILoaderScope extends ng.IScope
    {

    }

    export class LoaderDirective
    {
        private scope: ILoaderScope;

        constructor($scope: ILoaderScope, $rootElement: ng.IRootElementService, $attributes: ng.IAttributes, $compile: ng.ICompileService)
        {
            console.log("constructor");
            this.scope = $scope;
        }
    }

    let load = (): void =>
    {
        if (playground.core && playground.core.appCompileProvider)
        {
            playground.core.appCompileProvider.directive.apply(null, ["loader", ["$compile", ($compile: ng.ICompileService): ng.IDirective =>
            {
                return <ng.IDirective>{
                    restrict: "E",                  
                    templateUrl: "directives/loader/template.html",
                    scope: {
                    },
                    link: ($scope: ILoaderScope, $rootElement: ng.IRootElementService, $attributes: ng.IAttributes): LoaderDirective =>
                    {
                        return new LoaderDirective($scope, $rootElement, $attributes, $compile);
                    }
                };
            }]]);
        }
        else
        {
            setTimeout(load);
        }
    };

    load();
}

【问题讨论】:

    标签: javascript angularjs delayed-execution typescript1.8 angularjs-config


    【解决方案1】:

    试试这个,

    namespace playground.core
    {
        "use strict";
    
        export let playgroundApp: ng.IModule = angular.module("playgroundApp", []);
        export let compileProvider: ng.ICompileProvider = null;
    
        playgroundApp.config(($compileProvider: ng.ICompileProvider, $controllerProvider: ng.IControllerProvider) => {
            (<any>$controllerProvider).allowGlobals();
            compileProvider = $compileProvider;
        }); 
    
        angular.bootstrap(document, ["playgroundApp"]);
    }
    

    您不必依赖文档加载来引导 Angular。也许,这会延迟这个过程。

    【讨论】:

    • 感谢您,遗憾的是延迟仍然存在。根据我的阅读,这就是配置的工作方式
    猜你喜欢
    • 2015-03-08
    • 1970-01-01
    • 2015-07-23
    • 2013-09-11
    • 2013-04-14
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    相关资源
    最近更新 更多