Javascript AngularJS自定义指令templateURL
正在用AngularJS创建我的第一个自定义指令,但在使用Javascript AngularJS自定义指令templateURL,javascript,html,angularjs,filepath,Javascript,Html,Angularjs,Filepath,正在用AngularJS创建我的第一个自定义指令,但在使用templateURL参数时遇到了一些问题;它实际上并没有请求我试图调用的页面 当我使用普通的模板参数时,它会按预期工作。 我想我在模板URL中使用的路径可能不正确 这是我主页上的HTML: <div class="container" ng-controller="formFields"> <p>{{person.name}}</p> <div ng-sparkline pers
templateURL
参数时遇到了一些问题;它实际上并没有请求我试图调用的页面
当我使用普通的模板
参数时,它会按预期工作。
我想我在模板URL中使用的路径可能不正确
这是我主页上的HTML:
<div class="container" ng-controller="formFields">
<p>{{person.name}}</p>
<div ng-sparkline person="person"></div>
</div>
myApp.directive('ngSparkline', function() {
return {
replace: true,
restrict: 'EA',
scope: {
person: '='
},
//template: '<p class="lead">Hi, {{person.name}}!</p>'
templateURL: '/js/directives/formFields.html'
}
});
<p class="lead">Hey, {{person.name}}</p>
{{person.name}
这是我的自定义指令:
<div class="container" ng-controller="formFields">
<p>{{person.name}}</p>
<div ng-sparkline person="person"></div>
</div>
myApp.directive('ngSparkline', function() {
return {
replace: true,
restrict: 'EA',
scope: {
person: '='
},
//template: '<p class="lead">Hi, {{person.name}}!</p>'
templateURL: '/js/directives/formFields.html'
}
});
<p class="lead">Hey, {{person.name}}</p>
myApp.directive('ngSparkline',function(){
返回{
替换:正确,
限制:“EA”,
范围:{
人:'='
},
//模板:'Hi,{person.name}!
'
templateURL:“/js/directions/formFields.html”
}
});
这是我的formFields.html文件:
<div class="container" ng-controller="formFields">
<p>{{person.name}}</p>
<div ng-sparkline person="person"></div>
</div>
myApp.directive('ngSparkline', function() {
return {
replace: true,
restrict: 'EA',
scope: {
person: '='
},
//template: '<p class="lead">Hi, {{person.name}}!</p>'
templateURL: '/js/directives/formFields.html'
}
});
<p class="lead">Hey, {{person.name}}</p>
嘿,{{person.name}
这是我的应用程序目录的布局方式:
我的directives.js
文件位于js
目录中,而formFields.html
文件位于/js/directives/…
你的拼写(大小写)有错误templateUrl
,而不是templateUrl
您有拼写(大小写)错误templateUrl
,而不是templateUrl
一方面,您将templateUrl
误判为templateUrl
。
如果这不能解决您的问题,可能是因为绝对URL
您是通过服务器提供文件,还是通过磁盘在浏览器中打开文件
如果您的链接是file://C://www/html/js/directives
,那么绝对路径/js/directives
的目标显然是file://C:/js/directives
,其中没有模板代码。在这种情况下,您需要使用相对路径。例如,您将templateUrl
误判为templateUrl
。
如果这不能解决您的问题,可能是因为绝对URL
您是通过服务器提供文件,还是通过磁盘在浏览器中打开文件
如果您的链接是file://C://www/html/js/directives
,那么绝对路径/js/directives
的目标显然是file://C:/js/directives
,其中没有模板代码。在这种情况下,您需要使用相对路径。我编辑了我的答案。你刚刚拼错了TemplateUrl谢谢!真不敢相信我错过了!新手移动:)我编辑了我的答案。你刚刚拼错了TemplateUrl谢谢!真不敢相信我错过了!新手移动:)此应用程序托管在服务器上,因此根据应用程序中其他类似的相对路径构造,我知道我的文件路径是正确的。问题是拼写错误!由于此应用程序托管在服务器上,因此根据应用程序中其他类似的相对路径构造,我知道我的文件路径是正确的。问题在于拼写错误!哑口无言