1

Ich lerne AngularJS und TypeScript und baute eine sehr einfache Anwendung, um mich in Gang zu bringen.AngularJS 1.5 Config Delay

Eine Anwendung, die eine Elementrichtlinie auf eine Seite lädt, sehr einfach, aber ich treffe immer einen Fehler: "Uncaught TypeError: Kann die Eigenschaft 'Direktive' von null nicht lesen" beim Laden des Direktive-Hooks.

Ich verstehe, warum das passiert, aber ich kann keine Informationen im Internet finden, wie man das behebt.

Die TS als Referenz:

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> 

Die Ausnahme in directive.ts geworfen wird: playground.core.compileProvider .directive.apply ... weil es scheint eine Verzögerung auf playgroundApp zu sein .config (...), das nur aufgerufen wird, nachdem die Anweisung bereits geladen wurde.

Gibt es eine Möglichkeit, dieses Szenario zu arbeiten, ohne den Aufruf der Anweisung in eine setTimeout (...) zu wickeln?

Update:

So landete ich in einer Überprüfung der Richtlinie Teil Einwickeln und mit setTimeout sowieso, da es keine andere Lösung zu sein scheint.

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(); 
} 

Antwort

0

dies versuchen,

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"]); 
} 

Sie sollten nicht auf Dokument Laden verlassen Winkel Bootstrap. vielleicht verzögert das den Prozess.

+0

danke dafür, leider besteht die Verzögerung weiter. von dem, was ich gelesen habe, ist dies nur die Art, wie die Konfiguration funktioniert – JadedEric

+0

das ist seltsam ... –