从数据库Sqlite到AngularJS HTML

huangapple go评论71阅读模式
英文:

From Database Sqlite to AngularJS HTML

问题

我试图将来自数据库的HTML代码转换为我的页面。

@foreach (DataRow Row in DT.Rows)
{
    <p>@Row.ItemArray[1]</p>
    <span ng-bind-html="mydata"></span>
    <hr />
    <script type="text/javascript">
        var app = angular.module('magnos', ['ngSanitize']);
        app.controller('mycont', function ($scope) {
            $scope.mydata = '@Row.ItemArray[2]';
        });
        
    </script>
}

但是`@Row.ItemArray[2]`始终返回字符串,而不是像这样的HTML:`<h1>Magnos</h1>`。

我希望将HTML代码写成HTML代码在我的页面上,这样Magnos标题会显示为标题。
英文:

I trying to convert HTML code from database to my page.

@foreach (DataRow Row in DT.Rows)
{
    <p>@Row.ItemArray[1]</p>
    <span ng-bind-html="mydata"></span>
    <hr />
    <script type="text/javascript">
        var app = angular.module('magnos', ['ngSanitize']);
        app.controller('mycont', function ($scope) {
            $scope.mydata = '@Row.ItemArray[2]';
        });
        
    </script>
}

But the @Row.ItemArray[2] is always return string, not HTML like this
<h1>Magnos</h1>

I want the HTML code to write as HTML code in my page, so the Magnos header show as header.

答案1

得分: 0

感谢大家,我已经找到了解决方案,
以下是解决方案:-

@foreach (DataRow Row in DT.Rows)
{
    <h2>@Row.ItemArray[1]</h2>
    <br />
    <span ng-bind-html="get_pre('@Row.ItemArray[2]');"></span>
    <hr />
    <script type="text/javascript">
        var app = angular.module('magnos', []);
        app.controller('mycont', function ($scope, $sce) {
        
            $scope.get_pre = function (x) {
                return $sce.trustAsHtml(x);
            };
        });
    </script>
}
英文:

Thank you all I have found the solution,
Heres the Solution:-

@foreach (DataRow Row in DT.Rows)
{
    <h2>@Row.ItemArray[1]</h2>
    <br />
    <span ng-bind-html="get_pre('@Row.ItemArray[2]');"></span>
    <hr />
    <script type="text/javascript">
        var app = angular.module('magnos', []);
        app.controller('mycont', function ($scope, $sce) {
        
            $scope.get_pre = function (x) {
                return $sce.trustAsHtml(x);
            };
        });
    </script>
}

</details>



huangapple
  • 本文由 发表于 2023年6月5日 17:25:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定