我想将所有模板放入脚本标记中,并从我的指令中使用它们,而不需要使用 ajax 请求。
这是我的模板:
<script id="article_row.html" type="text/ng-template">
<tr>
<td class="article-id">[[ article.id ]]</td>
<td>[[ article.title ]]</td>
<td>[[ article.user_id ]]</td>
<td>[[ article.created_at ]]</td>
<td>
<article-restore id="article.id"></article-restore>
</td>
</tr>
</script>
这是我的指令:
app.directive("articleLine", [function(){
return {
return: 'E',
scope: {
article : "=data"
},
templateUrl: "article_row.html"
};
}]);
加载时,我在控制台中收到此错误:
Error: Failed to load template: article_row.html
我在控制台中看到浏览器正在尝试从http://localhost/articles/article_row.html
获取它
而是在页面中寻找它,我做错了什么吗?
谢谢!
最佳答案
<article-line data="myData"></article-line>
当您将 articelLine
指令放在上面时,此行将在您包含 article_row.html
ng-template 并收到错误之前进行编译。
因此,您必须在其前面放置 ng-template
。
关于javascript - AngularJS - 预加载模板不适用于指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22107656/