gpt4 book ai didi

javascript - ng-bind-html 的变量名称来自 ng-repeat 但变量在其他地方定义

转载 作者:行者123 更新时间:2023-11-28 06:10:40 25 4
gpt4 key购买 nike

嗨,我对 Angular 相当陌生,所以我不确定这是否是一个明显的答案,但我正在尝试通过 ng-repeat 将变量分配给 ng-bind-html。但是,当我通过 ng-repeat 添加变量时,Angular 只显示变量名称,因为它认为 html 内容来 self 的 ng-repeat 数据,而不是 Angular 中的其他位置。

我的html:

<div class="brandInfo" ng-repeat="brand in itemPage.brandinfo" ng-click="widgetExpanded = !widgetExpanded">
<div class="icon-wrapper">
<img ng-src="assets/img/icons/{{brand.icon}}"/>
</div>
<p> {{brand.title}} </p>
<div ng-slide-down="widgetExpanded" duration=".5">
<p ng-bind-html="brand.iconClass"></p>
</div>
</div>

我的 Controller :

vm.brandinfo = [
{icon: "organicCotton.svg", iconClass: "organicCotton", title: "Organic Cotton"},
{icon: "lowImpactDye.svg", iconClass: "lowImpactDye", title: "Low Impact Dyes"},
{icon: "factory.svg", iconClass: "factory", title: "Regulated Factory"},
{icon: "carbonFootprint.svg", iconClass: "carbonFootprint", title: "Sustainable Business Practices"}
];

$scope.organicCotton = $sce.trustAsHtml(

"<ul><li>Uses no pesticides or herbicides which a often toxic to humans and the environment</li> \
<li>Conventional cotton accounts for 25% of global pesticide usage</li> \
<li> Organic farming practices create healthy soils which make better use of water inputs and are more resilient in drought conditions</li> \
<li> The water pollution impact of organic has been shown to be 98% less than non-organic cotton production.</li></ul>"

);

所以基本上我为所有 iconClass 可能性设置了这些范围变量,即使我可以将正确的变量名称(brand.iconClass)放入 ng-bind-html 指令中。它仅评估为“organicCotton”并且不识别它是一个变量。

预先感谢您的帮助。如果我可以进一步澄清,请告诉我!

最佳答案

请尝试这个:

vm.organicCotton = $sce.trustAsHtml(

"<ul><li>Uses no pesticides or herbicides which a often toxic to humans and the environment</li> \
<li>Conventional cotton accounts for 25% of global pesticide usage</li> \
<li> Organic farming practices create healthy soils which make better use of water inputs and are more resilient in drought conditions</li> \
<li> The water pollution impact of organic has been shown to be 98% less than non-organic cotton production.</li></ul>"

);

vm.brandinfo = [
{icon: "organicCotton.svg", iconClass: vm.organicCotton, title: "Organic Cotton"},
{icon: "lowImpactDye.svg", iconClass: vm.lowImpactDye, title: "Low Impact Dyes"},
{icon: "factory.svg", iconClass: vm.factory, title: "Regulated Factory"},
{icon: "carbonFootprint.svg", iconClass: vm.carbonFootprint, title: "Sustainable Business Practices"}
];

因此,将变量添加到数组中,而不是将变量名称作为字符串添加。

关于javascript - ng-bind-html 的变量名称来自 ng-repeat 但变量在其他地方定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36434986/

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