1
我正在学习AngularJS和TypeScript,并构建了一个非常基本的应用程序来让我自己去。AngularJS 1.5配置延迟
一个将元素指示加载到页面上的应用程序非常简单,但我一直打错:“Uncaught TypeError:尝试加载指令挂接时无法读取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();
}
感谢这一点,不幸的是延迟仍然存在。从我读过的,这只是配置的工作方式 – JadedEric
这很奇怪... –