繁体   English   中英

如何使用Angularjs将数据库中的字符串转换为XML

[英]How to get a string from the DB into XML with Angularjs

我有来自数据库的这个xml数据字符串。

var svgString = '
<defs>
  <clipPath id="path-print-area-path">
    <path d="M767.5,474.5h-575a50,50,0,0,1-50-50V99.5a50,50,0,0,1,50-50h575a50,50,0,0,1,50,50v325A50,50,0,0,1,767.5,474.5Z" fill="transparent"></path>
  </clipPath>
  <clipPath id="path-top-FrontEdge">
    <path d="M817.5 512 142.5 512 142.5 500 480 500 817.5 500 817.5 512Z" fill="#FF0000"></path>
  </clipPath>
</defs>
<g id="Content" clip-path="url(http://maus.com/builder/2/14#path-print-area-path)" style="display: block;">
  <image xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/link-to-image.png" x="164" y="40" width="258" height="213" class="" style="cursor: move;"></image>
  <g id="Content-Text">
    <text font-family="calibri" font-size="45pt" fill="#FF0000" x="480" y="252" style="cursor: move;">
      <tspan style="basline-shift: 45;">Maus</tspan>
    </text>
    <text font-family="calibri" font-size="18pt" fill="#FF0000" x="480" y="277" style="cursor: move;">
      <tspan style="basline-shift: 18;">Master Chief</tspan>
    </text>
  </g>
</g>';

我试图通过使用angular提供的ngBindHtml指令将其放入我的视图中:(ng-bind-html =“item.SVG”)。

<svg ng-bind-html="item.SVG" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 960 560">
</svg>

当我这样做时,它取出了使svg像clip-path元素和属性一样工作所需的一切。 示例如下:

<svg ng-bind-html="x.Item.SVG" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 960 560">
  <defs>
    <!-- this is suppose to have <clip-path id="path-print-area-path"> -->
      <path d="M767.5,474.5h-575a50,50,0,0,1-50-50V99.5a50,50,0,0,1,50-50h575a50,50,0,0,1,50,50v325A50,50,0,0,1,767.5,474.5Z" fill="transparent">
      </path>
    <!-- </clip-path> -->
    <!-- this is suppose to have <clip-path id="path-top-FrontEdge"> -->
      <path d="M817.5 512 142.5 512 142.5 500 480 500 817.5 500 817.5 512Z" fill="#FF0000">
      </path>
    <!-- </clip-path> -->
  </defs>
  <g id="Content"> <!-- this is suppose to have these attributes; clip-path="url(http://maus.com/builder/2/14#path-print-area-path)" style="display: block;" -->
     <image xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/mm/Axomo/4179/users/FREDRICKJO/5_140732_4-dragon%20logo.png" x="164" y="40" width="258" height="213" class=""></image>
     <g id="Content-Text">
       <text font-family="calibri" font-size="45pt" fill="#FF0000" x="480" y="252">
         <tspan>Maus</tspan>
       </text>
       <text font-family="calibri" font-size="18pt" fill="#FF0000" x="480" y="277">
         <tspan>Master Chief</tspan>
       </text>
     </g>
   </g>
 </svg>

我正在寻找一种方法来获取所有信息到DOM中以正确显示我的SVG。 我在谷歌搜索和搜索了所有的SO,我最接近的是上面的例子。

请指出我正确的方向。

像这样使用ngSanitize$sce.trustAsHtml的组合

app.controller("SVGController", ["$scope", "$sce", function($scope, $sce) {

    $scope.item = {
        TrustedSVG: $sce.trustAsHtml(svgString)
    };

}]);

小提琴: https//jsfiddle.net/codeandcloud/ew2y4pb0/

暂无
暂无

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

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