以下是我用 typescript 类编写的 Angular Directive(指令)代码。 当我在浏览器中运行指令时。我收到错误
Failed to load template: ../Templates/inputcontrol.html (HTTP status: 404 Not Found) - Error: $compile:tpload Error Loading Template
我正在使用 asp.net mvc
如何在angular中设置路径或templateUrl?
<input-control a="shan"></input-control>
虽然相对路径没问题。
class InputControl implements ng.IDirective {
restrict = "E";
scope = {
a: "=a"
};
templateUrl = "../Templates/inputcontrol.html";
controller = ["$scope", ($scope: any) => {
console.log("this this controller", $scope.a);
}];
controllerAs = "inputcontroller";
constructor(private $location: ng.ILocationService) {
}
link = (scope: ng.IScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes, ctrl: any) => {
console.log(this.$location);
};
static factory(): ng.IDirectiveFactory {
const directive = ($location: ng.ILocationService) => new InputControl($location);
directive.$inject = ["$location"];
return directive;
}
}
angular.module("SheetApp").directive("inputControl", InputControl.factory());
最佳答案
不要使用相对路径 - 而是根据您在执行 Get 请求时期望如何从服务器检索元素的路径。
假设 AngularApp 位于项目的顶层(默认情况下与 Controllers 或 Scripts 文件夹相同的树状缩进),使用以下模板 URL 而不是相对路径。
“/AngularApp/Templates/main.html”
关于javascript - 无法从 Typescript 类中的 Angular Directive(指令)加载模板 (404),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34254901/