【发布时间】:2015-01-26 17:45:07
【问题描述】:
我有一个模板,可重复使用以显示具有不同数据的行。该模板还包含一个与该行相关的$scope 函数。
<script id="wineRowTemplateT" type="text/html" style="display: none;">
<div class="row tasting" style="margin-bottom: 15px;">
<div class="col col-10">
<div class="rating" style="display: block; width: 60px; height: 60px; text-align: center; line-height: 60px; font-weight: 700; border: thin solid #ccc; border-radius: 50%; font-size: 1.2em;">{tasting_points}p</div>
</div>
<div class="col col-90">
<div class="row info">
<div class="col col-70">
<h3 style="font-weight: 300; border-bottom: thin solid #000; display: inline-block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">
<span ng-click="smGoto('http://dev.thetastingbook.com/p/{house_url}', 'external')" style="font-weight: 600;">{house_name}</span>
<span ng-click="smGoto('http://dev.thetastingbook.com/wine/{house_url}/{wine_url}', 'external')">{wine_name}</span> {wine_vintage}
</h3>
</div>
<div class="col col-30">
<h4 ng-click="showTastingAdjectivesModal($event)" data-tasting-id="{tasting_id}" data-wine-id="{wine_id}" style="font-weight: 300; text-transform: uppercase; text-decoration: underline;">Show tasting note</h4>
</div>
</div>
</div>
</div>
</script>
有问题的函数是showTastingAdjectivesModal()
在我的测试用例中,我从模板中创建了六行。当我尝试在其中一行上点击 Show Tasting Note 时,它会触发六次。 testingId 和 wineId 来自正确的行,只是它都被触发了多次(六)次,我不知道是什么原因造成的。
这是我的 showTastingAdjectivesModal() 函数:
$scope.showTastingAdjectivesModal = function (cEvent) {
console.log("showNoteModal called");
var $this = $(cEvent.toElement);
var tastingId = $this.data("tastingId");
var wineId = $this.data("wineId");
Tasting.getTastedWineAdjectives(tastingId, wineId)
.then(function(adjectives){
console.log("Got adjectives", JSON.stringify(adjectives));
adjectives.forEach(function(adj){
var type = adj.type;
if(type == "drink")
type = "conclusion";
$(".tasting-adjective-row[data-type='"+type+"']").find(".adjectives").append(adj.name + ", ");
});
$scope.noteModal.show();
});
};
console.log 我每次点击它都会打印 6 次。
附带说明,此代码在 iOS 设备上使用 Cordova 和 Ionic/AngularJS 运行。
谁能告诉我我做错了什么?
下面是模板的用法:
var res = obj.res;
var stats = obj.stats;
section = {abbr: "", proper: ""};
tastingDate = moment(tasting.tasting_date);
if (tastingDate.isAfter(moment().startOf("isoweek")) || tastingDate.isSame(moment().startOf("isoweek"))) {
section.abbr = "week";
section.proper = "This Week";
} else if (tastingDate.isAfter(moment().subtract(1, "weeks").startOf("isoweek")) || tastingDate.isSame(moment().subtract(1, "weeks").startOf("isoweek"))) {
section.abbr = "lastweek";
section.proper = "Last Week";
} else {
section.abbr = tastingDate.format("W");
section.proper = "Week " + tastingDate.format("W");
}
var wineRowTemplate = $("#wineRowTemplateT").html();
wineRowTemplate = wineRowTemplate.split("{tasting_id}").join(tasting.id);
wineRowTemplate = wineRowTemplate.split("{wine_id}").join(res.id);
wineRowTemplate = wineRowTemplate.split("{tasting_points}").join(tasting.score);
wineRowTemplate = wineRowTemplate.split("{house_name}").join(res.house_name);
wineRowTemplate = wineRowTemplate.split("{house_url}").join(res.winehouse_url_name);
wineRowTemplate = wineRowTemplate.split("{wine_name}").join(res.name);
wineRowTemplate = wineRowTemplate.split("{wine_url}").join(res.wine_url_name);
wineRowTemplate = wineRowTemplate.split("{wine_vintage}").join(res.vintage);
wineRowTemplate = wineRowTemplate.split("{last_tasted}").join(tastingDate.fromNow());
wineRowTemplate = wineRowTemplate.split("{overall_total}").join(stats.total);
wineRowTemplate = wineRowTemplate.split("{overall_average}").join(stats.average);
wineRowTemplate = wineRowTemplate.split("{overall_best}").join(stats.highest);
wineRowTemplate = wineRowTemplate.split("{overall_lowest}").join(stats.lowest);
wineRowTemplate = wineRowTemplate.split("{tb_point}").join(parseInt(res.tb_points, 10).toFixed(2));
wineRowTemplate = wineRowTemplate.split("{wine_note}").join(tasting.note);
if (!$sections.find(".section[data-section='" + section.abbr + "']").length) {
var sectionTemplate = $("#sectionTemplate").html();
sectionTemplate = sectionTemplate.split("{section_name_abbr}").join(section.abbr);
sectionTemplate = sectionTemplate.split("{section_name_proper}").join(section.proper);
$sections.append(sectionTemplate);
}
console.log("[TASTING] Inserting data to view for wine ID", tasting.wine_id);
$compile($sections.find(".section[data-section='" + section.abbr + "'] .tastings").prepend(wineRowTemplate))($scope);
更新:我注意到如果例如我单击 DOM 中的第一行 ng-click,该函数仅触发一次,但如果我单击例如在 DOM 中的第二个,该函数被触发两次。因此,由于某种原因,每个连续行的绑定都会增加对该行的绑定 ng-click。我检查了我的 html 结构,以确保它没有丢失/不必要的结束标签,但事实并非如此。
【问题讨论】:
-
展示你是如何使用模板的。
-
在标准浏览器上的行为是否相同?您可以做的一件事是在该功能上放置一个“锁”,使其每秒只能点击一次。
-
我添加了模板用法以及我发现的一些其他信息
-
我还没有尝试在浏览器上测试这个。虽然我确实有另一个使用相同原理的视图:包含带有 ng-click 的函数的已定义模板,用法与上述相同。但不同的是,每次点击都会正确触发点击。
标签: angularjs cordova ionic-framework