我正在尝试构建一个非常简单的 Angular 指令:
app.directive("myDirective", function () {
return {
template: "{{result}} {{result.startTime}}",
scope: {
result: '@result'
}
};
});
我在 View 中这样使用它:
<div my-directive result="{{result}}"></div>
问题是 {{result}}
正确显示(作为 json)abject,而 {{result.startTime}}
未显示,尽管事实上,显示的 {{result}}
包含 startTime
属性。
最佳答案
2个问题:
1 您需要传入模型,而不是内插的字符串。
<div my-directive result="result"></div>
2 您需要将值分配给指令,因此请使用“=”而不是“@”,这将为您提供从指令到仅 DOM 的单向绑定(bind)。
app.directive("myDirective", function () {
return {
template: "{{result}}, {{result.startTime}}",
scope: {
result: '='
}
};
});
关于javascript - AngularJS 自定义指令显示 {{obj}} 但不显示 {{obj.prop}}?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18597158/