简体   繁体   English

Angular为什么找不到我的模板?

[英]Why can't Angular find my template?

I'm trying to do the phoneCat tutorial for angular JS, except in an MVC application. 我正在尝试为angular JS做phoneCat教程 ,除了在MVC应用程序中。 (I hear it's not a great idea to mix MVC routing and Angular routing, but I haven't gotten that far yet.) I'm on step 4, where we split our components off into their own modules. (我听说混合MVC路由和Angular路由不是一个好主意,但是我还没走那么远。)我在第4步,在这里我们将组件拆分成自己的模块。 I made a folder in my app called "AngularModules" and made a folder in that called "phone-list". 我在应用程序中创建了一个名为“ AngularModules”的文件夹,并在该文件夹中创建了一个名为“ phone-list”的文件夹。 I then placed the phone-list.component.js and phone-list.module.js files we make in the tutorials into that folder. 然后,我将在教程中制作的phone-list.component.js和phone-list.module.js文件放入该文件夹中。 The next step calls for replacing the HTML string with a reference to an external file that will contain the html. 下一步需要用对包含html的外部文件的引用替换HTML字符串。 but for some reason, angular can't seem to find that file. 但是由于某种原因,Angular似乎找不到该文件。 If I view the template file and press f5, I do start up and go to a view of that file (with obviously no binding done, so it shows the double curly braces and all). 如果查看模板文件并按f5键,则会启动并转到该文件的视图(显然没有完成绑定,因此它显示了双花括号和所有花括号)。 I'm quite certain the folder isn't the problem, because if I replace the templateURL part with a template part with a string, it will work. 我相当肯定的文件夹是没有问题的,因为如果我更换templateURL与部分template用字符串的一部分,它会工作。 so here's my phone-list.component.js: 所以这是我的phone-list.component.js:

angular.module("phoneList").component("phoneList", {
    //templateURL: "http://localhost:52413/AngularModules/phone-list/phone-list.template.html",
    templateURL: "AngularModules/phone-list/phone-list.template.html",
    //template:"<ul>" + 
    //      '<li ng-repeat="phone in $ctrl.phones">' +
    //        "<span>{{phone.name}}</span>" + 
    //        "<p>{{phone.snippet}}</p>" +
    //      "</li>" + 
    //    "</ul>",
    controller: function PhoneListController() {
        this.phones = [
            {
                name: 'Nexus S',
                snipped: 'Fast just got faster with Nexus S.'
            }, {
                name: 'Motorola XOOM™ with Wi-Fi',
                snippet: 'The Next, Next Generation tablet.'
            }, {
                name: 'MOTOROLA XOOM™',
                snippet: 'The Next, Next Generation tablet.'
            }
        ];
    }
});

I'm not worried I'm not including phone-list.component.js correctly; 我不担心我没有正确包含phone-list.component.js。 if I use the template section (currently commented out) it works. 如果我使用template部分(当前已注释掉),则它可以工作。 That other URL, which points to localhost, also doesn't work. 指向localhost的其他URL也无效。 But if I click the link, I do see the page. 但是,如果我单击链接,则会看到该页面。 I have tried templateURL: "~/AngularModules/phone-list/phone-list.template.html" , which also didn't work. 我尝试了templateURL: "~/AngularModules/phone-list/phone-list.template.html" ,它也没有用。

How can I get templateURL to work? 我如何才能使用templateURL?

According the docs, the property name is templateUrl and not templateURL , so, your code should looks like this: 根据文档,属性名称是templateUrl而不是templateURL ,因此,您的代码应如下所示:

angular.module("phoneList").component("phoneList", {
    templateUrl: "AngularModules/phone-list/phone-list.template.html",

Now you probably needs to handle the template path issue, but you will be in the correct path. 现在您可能需要处理模板路径问题,但是您将处于正确的路径。

Check info about components here 在此处查看有关组件的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM