javascript - 无法从 Typescript 类中的 Angular Directive(指令)加载模板 (404)

标签 javascript angularjs asp.net-mvc angularjs-directive typescript

以下是我用 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>

虽然相对路径没问题。

enter image description here

  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/

相关文章:

javascript - angularjs:在指令内创建指令

JQuery ajax POST字符串参数,MVC操作方法获取null

c# - 如何将 kendoui 下拉列表的选定值绑定(bind)到我的模型?

javascript - 意外的 token ,预期的......

javascript - 保持文本字段处于焦点状态

javascript - 使用 AngularJS 开发完整的面向公众的企业应用程序

javascript - angularjs 指令检查是否最小。复选框列表中的一项被选中

asp.net-mvc - SimpleMembership MVC 4从在webpages_Roles中具有角色的UserProfile获取数据

javascript - 从大文件流式传输并创建数组

javascript - 设置复选框列表的样式