繁体   English   中英

从angularjs过滤器输出HTML而不使用ng-bind-html?

[英]Output HTML from angularjs filter without ng-bind-html?

好。 我已经用Google搜索了没有运气的可能性。

这是我的情景:

我正在尝试显示一个微调器,等待任何值等待解析的承诺,但是我想使用过滤器来实现这一点而不使用ng-bind-html指令,因为我的大多数绑定都是使用花括号完成的表达式语法: {{myvalue}} 我只想在需要此行为的地方添加过滤器。 像这样: {{myvalue | waiting}} {{myvalue | waiting}} ,以便每当myvalue的promise解析时都可以替换它。

我搜索过,发现没有ng-bing-html指令就无法输出html。 但我只是在检查是否有人知道更好的方法来实现它,只需将waiting标记作为attribute/filter/css class放在我需要此行为的任何地方。

过滤代码:

app.filter('waiting', function(){
    return function(value){
        return '<img src="loading.png"/>';
    }
});

示例HTML:

<div ng-controller='ControllerThatHoldsPromise'> <!-- :) -->
    <span>{{myvalue | waiting}}</span>
</div>

总而言之,我的目标是输出没有ng-bind-html的html。 谢谢

因此,这项研究向我证明,你必须绝对使用ng-bind-html指令在Angular中输出html。 我真的只想使用一个过滤器来解决问题,只需在等待承诺解析时将html内容分配给控制器变量,但根据@ErikLundgren的建议,我决定使用带有伪元素的ng-class实现它。

这就是我的解决方案的成果......

控制器:

var app = angular.module('MyApp.controllers', ['ngSanitize']);
app.controller('SnazzyController', ['$scope','$timeout', function($scope,$timeout){

    $scope.new_orders = 0;
    $scope.dataPending = true;

    //simulate a delayed response
    $timeout(
        function(){
                $scope.new_orders = 20;
            }
            $scope.dataPending = false;
        }, 4000);
}]);

CSS:

.result-pending{
    position: relative;
}

.result-pending::before{
    content: "";
    position: absolute;
    z-index: 9999;
    height: 100%;
    width: 100%;
    min-height: 64px;
    min-width: 64px;
    background: #FFF url("/images/lazyLoader2.gif") center no-repeat;
    background-size: contain;
    left: 0;
    top: 0;
}

标记:

<div class="panel panel-primary">
    <div class="panel-heading">Waiting Demo</div>
    <div class="panel-body">
        <div class="data" ng-class="{'result-pending': dataPending}">
            {{new_orders | number:0}}
        </div>
        <div class="title">
            NEW ORDERS
        </div>
    </div>
</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM