簡體   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