在开发指令时,最好的做法是将数据/模型与实际指令分开。
例如,如果我有一个名为“Event”的指令,例如
<div class="event">
<h1>{event.title}</h1>
<small>{event.startDate}</small>
</div>
以最可重用的模式向指令提供数据的最佳方式是什么?通过服务?
最佳答案
指令可以做很多事情。您可以通过多种方式处理数据。更直接的方法是将数据或引用传递到您的指令中。查看下面的链接以供引用。作为您的代码的示例,我将执行以下操作:
<div dir-event event-title="context.title" event-start="context.startDate" ></div>
在 HTML 中,context.title 和 context.startDate 是 Controller 上的对象。然后对于指令:
App.directive("dirEvent", function() {
return{
restrict: "A",
scope:{
title:"=",
startDate:"="
},
transclude: true,
template: "<div class='event'><h1>{{title}}</h1><small>{{startDate}}</small></div>",
replace: true
}
});
关于javascript - 向 AngularJS 中的指令提供远程数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17823443/