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

时间:2016-04-05 19:07:26

标签: javascript angularjs angularjs-scope angularjs-ng-repeat ng-bind-html

嗨,我对角度很新,所以我不确定这是否是一个明显的答案,但我试图通过ng-repeat为ng-bind-html分配一个变量。然而,当我通过ng-repeat添加变量时,angular只显示变量名称,因为它认为html内容来自我的ng-repeat数据,而不是其他角度。

我的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>

我的控制器:

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指令中。它仅评估为&#34; organicCotton&#34;并没有意识到它是一个变量。

提前感谢您的帮助。如果我能进一步澄清,请告诉我!

2 个答案:

答案 0 :(得分:0)

请试试这个:

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"}
            ];

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

答案 1 :(得分:0)

您可以将html定义为对象品牌的关键字,直接从品牌中使用它而不是使其复杂化。

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"}
            ];

vm.brandInfo[0].iconClass = $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>"

            );

仅针对一个数组元素的示例。