简体   繁体   English

在过滤器中注入HTML-angularJS

[英]inject HTML in filters - angularJS

I am trying to build an angular app where i display a list of countries. 我正在尝试构建一个显示国家列表的角度应用程序。 In my db the countries are listed without articles (ie "US" instead of "the US") 在我的数据库中,列出的国家没有文章(即“美国”而不是“美国”)

How ever I'd like to display them in a paragraph so I created a filter to correct this: 我想如何在段落中显示它们,所以我创建了一个过滤器来更正此问题:

startupjobsApp.filter('countryFilter', function() {
  return function(input) {
    switch(input)
    {
    case 'United Kingdom':
        return 'the ' + input;
    case 'United States':
        return 'the ' + input;
    default:
        return input;
    }
  };
});

And I display them using this code: 我使用以下代码显示它们:

<span class="content">
         <p>bla bla bla bla in <span>{{job.country | countryFilter}}</span></p>
</span>

My problem here is that my span has a different font color than the rest of the paragraph. 我的问题是我的跨度与段落其余部分的字体颜色不同。 I have thought of injecting the <span> in the filter (return 'the <span> ' + input + </span> ;) but the HTML is not compiled. 我曾考虑过将<span>注入到过滤器中(返回' <span> '+ input + </span> ;),但是HTML尚未编译。

How can I do this? 我怎样才能做到这一点?

Thanks 谢谢

If you want to inject HTML into a filter, you can: 如果要将HTML注入过滤器,则可以:

I believe you will need to download angular-sanitize. 我相信您将需要下载angular-sanitize。 ( http://docs.angularjs.org/api/ngSanitize/service/ $sanitize and http://code.angularjs.org/1.2.9/ ) http://docs.angularjs.org/api/ngSanitize/service/ $ sanitize和http://code.angularjs.org/1.2.9/

<span class="content">
     <p>bla bla bla bla in <span ng-bind-html="job.country | countryFilter"></span></p>
</span>

startupjobsApp.filter('countryFilter', function() {
  return function(input) {
    switch(input)
    {
    case 'United Kingdom':
        return '<span>the ' + input +'</span>';
    case 'United States':
        return '<span>the ' + input  +'</span>';
    default:
        return input;
    }
  };
});

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

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