简体   繁体   English

指令未在AngularJS上将HTML字符串作为实际HTML加载

[英]Directive not loading HTML string as actual HTML on AngularJS

I am new to angularjs and using this directive as below. 我是angularjs的新手,并按如下所示使用此指令。 When this directive gets loaded on HTML file, it should treat these HTML as actual HTML rather than string but it is not working. 当此指令加载到HTML文件时,应将这些HTML视为实际的HTML,而不是字符串,但是它不起作用。 I am using angular 1.4.7 version. 我正在使用角度1.4.7版本。

Please help!! 请帮忙!! I am adding below HTML as String because I am getting that HTML as String from service dynamically. 我在HTML下方添加String作为字符串,因为我是从服务动态获取HTML字符串形式的。 So this is just example that I am adding here to see how can we display html value on angularjs html if it is coming as string. 因此,这只是我在此处添加的示例,以查看如果它以字符串形式出现,如何在angularjs html上显示html值。

angular.module('my.directive', [])
    .directive("myDirective", function(){
        return {
            restrict: "EA",
            scope: false,
            template: "<div class='con'>"+
            "<div>'<p><i><strong>some text</strong></i></p>'</div>"+
            "</div>"
        };
    });

I have tried multiple ways to fix it here but no luck. 我尝试了多种方法在这里修复它,但是没有运气。 I have tried using "ng-bind-html-unsafe" and "ng-bind-html" but none of them works correctly. 我曾尝试使用“ ng-bind-html-unsafe”和“ ng-bind-html”,但它们均无法正常工作。

I have even tried using direct HTML with ng-bind-html and unsafe as well but no luck there as well. 我什至尝试过将直接HTML与ng-bind-html一起使用,并且也不安全,但也没有运气。

I have simply tried below HTML but that doesn't work as well. 我只是尝试在HTML下面使用,但效果不佳。

<div ng-repeat="list in lists">
    <div class="content">
        <div ng-bind-html='<p><i><strong>some text</strong></i></p>'></div>
    </div>
</div>

Also below doesn't work. 也下面不起作用。

<div ng-repeat="list in lists">
    <div class="content">
        <div ng-bind-html-unsafe='<p><i><strong>some text</strong></i></p>'></div>
    </div>
</div>

I did some modifications in your code. 我对您的代码做了一些修改。

template: "<div class='con'>" +
                "<div><p><i><strong>some text</strong></i></p></div>" +
                "</div>"

AnuglarJS - Directives AnuglarJS-指令

The restrict option is typically set to: 限制选项通常设置为:

  • 'A' - only matches attribute name 'A'-仅匹配属性名称
  • 'E' - only matches element name 'E'-仅匹配元素名称
  • 'C' - only matches class name 'C'-仅匹配类别名称

In your directive, you have this restriction: restrict: "EA" , then you can use as element: 在您的指令中,您有以下限制: restrict: "EA" ,那么您可以将其用作元素:

<my-directive></my-directive> . <my-directive></my-directive>

 (function() { angular.module('my.directive', []) .directive("myDirective", function() { return { restrict: "EA", scope: false, template: "<div class='con'>" + "<div><p><i><strong>some text</strong></i></p></div>" + "</div>" }; }) .controller("Controller", ["$scope", function($scope) { $scope.title = "my-directive"; $scope.lists = [{ "id": 1, "text": "1" }, { "id": 2, "text": "2" }]; } ]); })(); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div data-ng-app="my.directive"> <div data-ng-controller="Controller"> <h1 data-ng-bind="title"></h1> <div data-ng-repeat="list in lists"> <div class="content">{{list.id}} <my-directive></my-directive> </div> </div> </div> </div> 

jsFiddle


Update: Using ngSanitize : 更新:使用ngSanitize

https://docs.angularjs.org/api/ngSanitize https://docs.angularjs.org/api/ngSanitize

In your current markup, you need to add double quoutes " . 在当前的标记中,您需要添加双倍法定字符"

<div data-ng-bind-html="'<p><i><strong>some text</strong></i></p>'"></div>

And add the ngSanitize dependency in your module. 并在模块中添加ngSanitize依赖项。

Something like this: 像这样:

 (function() { angular.module("my.directive", ["ngSanitize"]) .controller("Controller", ["$scope", function($scope) { $scope.title = "my-directive"; $scope.lists = [{ "id": 1, "text": "1" }, { "id": 2, "text": "2" }]; } ]); })(); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <script src="https://code.angularjs.org/1.4.7/angular-sanitize.min.js"></script> <div data-ng-app="my.directive"> <div data-ng-controller="Controller"> <h1 data-ng-bind="title"></h1> <div ng-repeat="list in lists"> <div class="content"> <div data-ng-bind-html="'<p><i><strong>some text</strong></i></p>'"></div> </div> </div> </div> </div> 

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

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