简体   繁体   English

AngularJS自定义指令templateURL

[英]AngularJS Custom Directive templateURL

Working on creating my first custom directive in AngularJS, but am having some problems when using the templateURL parameter; 正在使用AngularJS创建我的第一个自定义指令,但是在使用templateURL参数时遇到了一些问题; it doesn't actually request the page that I am attempting to call. 它实际上并没有请求我尝试调用的页面。

When I use the plain template parameter, it works as expected though. 当我使用普通template参数时,它可以按预期工作。 I think that maybe my path being used in the templateURL may be incorrect?? 我认为在templateURL中使用的路径可能不正确?

This is my HTML in my main page: 这是我主页上的HTML:

<div class="container" ng-controller="formFields">
    <p>{{person.name}}</p>
    <div ng-sparkline person="person"></div>
</div>

Here is my custom directive: 这是我的自定义指令:

myApp.directive('ngSparkline', function() {
  return {
    replace: true,
    restrict: 'EA',
    scope: {
        person: '='
    },
    //template: '<p class="lead">Hi, {{person.name}}!</p>'
      templateURL: '/js/directives/formFields.html'
  }
});

Here is my formFields.html file: 这是我的formFields.html文件:

<p class="lead">Hey, {{person.name}}</p>


This is how my app directory is laid out: My directives.js file is located in the js directory, while the formFields.html file is located in /js/directives/... 这是我的应用程序目录的布局方式:我的directives.js文件位于js目录中,而formFields.html文件位于/js/directives/...
目录布局

For one, you have miscased templateUrl as templateURL . 一方面,您将templateUrl大小写错误设置为templateURL If this does not fix your problem, it might be because of the absolute URL. 如果这不能解决您的问题,则可能是因为绝对URL。

Are you serving the file via a server or did you open it in a browser via the disk? 您是通过服务器提供文件还是通过磁盘在浏览器中打开文件?

If your link is file://C://www/html/js/directives , the absolute path /js/directives targets file://C:/js/directives , where no template code lies, obviously. 如果您的链接是file://C://www/html/js/directives ,则绝对路径/js/directives file://C:/js/directives ,显然,其中没有模板代码。 In that case you need to use relative paths. 在这种情况下,您需要使用相对路径。

You've got a spelling(case) mistake. 您有一个拼写(大小写)错误。 templateUrl , not templateURL templateUrl ,而不是templateURL

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

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