javascript - AngularJS - 预加载模板不适用于指令

标签 javascript angularjs angularjs-directive angular-template

我想将所有模板放入脚本标记中,并从我的指令中使用它们,而不需要使用 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/

相关文章:

angularjs - 如何更新 Angular meteor 中的用户字段?

具有隔离范围的 AngularJS 指令 - 默认属性值

javascript - AngularJS:如何将常量对象绑定(bind)到指令

javascript - 如何在光标坐标处打开自己的上下文菜单?

javascript - jQuery .on() 是否支持 "load"或 "ready"事件?

angularjs - Angular JS viewContentLoaded 在初始主页加载时加载两次

javascript - 打开时弹出窗口不显示

javascript - 在自定义指令中调用服务

javascript - 如何使用 Fetch 将附加数据从客户端 Stripe Checkout 传递到服务器

javascript - 知道链接是否已经打开