gpt4 book ai didi

javascript - 如何在指令模板中使用传递的数据?

转载 作者:行者123 更新时间:2023-11-28 18:56:56 27 4
gpt4 key购买 nike

我创建了一个可重用的组件,其指令如下:

app.directive("menu", function () {
return {
restrict: 'AE',
templateUrl: '/menu.html',
scope: {
title: '=title'
}
};
});

菜单.html:

<div>{{title}}</div>

显然我想从外部传递 title 属性,并且 show 位于 menu.html 内。

main.html 中的用法:

<div menu title="test"></div>

结果:标签未解析。为什么?

最佳答案

你必须这样声明:

app.directive("menu", function () {
return {
restrict: 'AE',
templateUrl: '/menu.html',
scope: {
title: '@'
}
};
});


Text Binding (Prefix: @)
One-way Binding (Prefix: &) (for functions)
Two-way Binding (Prefix: =)

This SO post更深入地了解@ vs =

关于javascript - 如何在指令模板中使用传递的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33431713/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com