【问题标题】:ng-bind-html with variable name coming from ng-repeat but variable defined elsewhereng-bind-html 变量名来自 ng-repeat 但变量在别处定义
【发布时间】:2016-04-05 19:07:25
【问题描述】:

您好,我对 Angular 比较陌生,所以我不确定这是否是一个明显的答案,但我正在尝试通过 ng-repeat 将变量分配给 ng-bind-html。但是,当我通过 ng-repeat 添加变量时,angular 仅显示变量名称,因为它认为 html 内容来自我的 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>

我的控制器:

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”并且不承认它是一个变量。

提前感谢您的帮助。让我知道我是否可以进一步澄清!

【问题讨论】:

  • 如果你定义了“controller as itemPage”,你不能使用 $scope 模式以及相同的 ng-controller 绑定。这就是你在做什么?
  • 是的,我同时使用 vm 和 $scope

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


【解决方案1】:

请试试这个:

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

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

【讨论】:

  • 我试过了,它看起来就像

    段落是空的
  • 抱歉更正它说

  • 奇怪。我做了一个Plnkr,效果很好。也许在脚本的其他地方?你在控制器中注入 $sce 了吗?
  • 所以我查看了你的 plunkr,我意识到我在 vm.brandinfo 之后定义了包含 html 的变量,当我在它工作之前定义它们时!谢谢!
【解决方案2】:

您可以改为将 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>"

            );

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

【讨论】:

  • 问题是我有多个品牌,并且 vm.brandinfo 是使用 switch 语句分配的。因此,如果有意义的话,iconClass 将不会总是相同或顺序相同
猜你喜欢
  • 1970-01-01
  • 2015-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-29
  • 1970-01-01
  • 2015-02-09
相关资源
最近更新 更多