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

标签 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 指令将其放入我的 View 中,如下所示:(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 工作所需的所有内容,如剪辑路径元素和属性。下面的例子:

<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。我在谷歌上搜索了一遍又一遍,我得到的最接近的是上面的例子。

请给我指出正确的方向。

最佳答案

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

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

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

}]);

fiddle :https://jsfiddle.net/codeandcloud/ew2y4pb0/

关于javascript - 如何使用 Angularjs 将数据库中的字符串转换为 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38003443/

相关文章:

javascript - js游戏中的相机,跟随玩家

javascript - JSON 数据作为字符串工作,但在对象时抛出内部错误

javascript - 如果输入文件在没有表单的ajax请求中作为数组元素传递,为什么数组对象变为空

Angularjs:测试 Controller 功能,它改变了 $scope

xml - 如何为此 xml 创建 xsd 架构?

javascript - 两次相同的 onclick 函数 - 只有一个有效

javascript - 极限阵列, Angular

angularjs - 如何动态禁用 ng-repeat 内的过滤器

android - 从大型 XML 填充 ListView

java - 检查字符串是否是 XML 文档的有效标记/属性名称