0

Got some problem that I'm solving now using filters, but I'm sure there must be a clever and simpler way.

Got this view:

<ul id="FilesView" class="FilesView">
    <li ng-repeat="object in folder.content.object" class="{{object.type}}">
        <img src="object.thumbnails.list | thumbPrinter">
        {{object.name}}
    </li>
</ul>

And a filter

angular.module('appFilters', []).filter('thumbPrinter', function() {
    if (media.type === 'file') {
        media.src   
    }else{
        '/images/folder.jpg'
    }
});

And XML

<object>
    <type>folder</type>
    <name>Photos</name>
    <complete_path>/Photos</complete_path>
    <complete_path_hash>00e474bd8bd7deaff259</complete_path_hash>
    <date_created>2013-03-06 16:33:28</date_created>
    <date_updated>2013-03-06 16:33:28</date_updated>
    <deleted>false</deleted>
</object>

Now this is working. but it is ugly, and has its problems.

Problems like- Lets say that I wish to print img tag, only if I got an img src, and span with folder as bg img. In currect senario I just can't.

How can I inject HTML as I wish, and not only strings? What is the correct way of doing that?

Thank you.

4

2 回答 2

1

你可以使用ngBindHtml。这将防止注入“不安全”的 HTML。您也可以使用ngBindUnsafeHtml,但这会使您面临各种危险,例如脚本注入等。

但从“角度方式”做事的角度来看,最好按照 Ketan 的建议去做。

请记住,ngBindHtml 存在于单独的 .js 文件中。

于 2013-05-13T21:52:56.743 回答
1

您可以使用 ng-show 或 ng-switch 根据条件显示不同的 HTML 块吗?

编辑:另一种选择是使用一个指令来确定要注入的 html 的类型。

于 2013-05-13T21:34:18.957 回答