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

时间:2016-06-23 23:26:58

标签: javascript angularjs xml svg

我有来自数据库的这个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。我在谷歌搜索和搜索了所有的搜索结果,而我最接近的就是上面的例子。

请指出正确的方向。

1 个答案:

答案 0 :(得分:0)

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

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

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

}]);

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