是否可以在AngularJS控制器中创建一个HTML片段,并在视图中显示该HTML?
这是因为需要将不一致的JSON blob转换为id:value对的嵌套列表。因此,HTML是在控制器中创建的,我现在希望显示它。
我已经创建了一个模型属性,但如果不打印HTML,就无法在视图中呈现它。
使现代化
问题似乎源于将创建的HTML作为引号中的字符串进行角度渲染。将尝试找到解决方法。
控制器示例:
var SomeController = function () {
this.customHtml = '<ul><li>render me please</li></ul>';
}
示例视图:
<div ng:bind="customHtml"></div>
给予:
<div>
"<ul><li>render me please</li></ul>"
</div>
幸运的是,您不需要任何花哨的过滤器或不安全的方法来避免错误消息。这是以预期和安全的方式在视图中正确输出HTML标记的完整实现。
Angular:
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular-sanitize.js"></script>
然后,必须加载模块:
angular.module('app', [
'ngSanitize'
]);
这将允许您在来自控制器、指令等的字符串中包含标记:
scope.message = "<strong>42</strong> is the <em>answer</em>.";
最后,在模板中,必须按如下方式输出:
<p ng-bind-html="message"></p>
这将产生预期的输出:答案是42。
使用管道在Angular 4的模板中显示html的工作示例。
1.板条箱管道擒纵机构-html.Pipe.ts
`
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
@Pipe({name : 'keepHtml', pure : false})
export class EscapeHtmlPipe implements PipeTransform{
constructor(private sanitizer : DomSanitizer){
}
transform(content){
return this.sanitizer.bypassSecurityTrustHtml(content);
}
}
`2.将管道注册到app.module.ts
import {EscapeHtmlPipe} from './components/pipes/escape-html.pipe';
declarations: [...,EscapeHtmlPipe]
在模板中使用<div class=“demoPipe”[inerHtml]=“getDivHtml(obj.header)|keepHtml”>getDivHtml(){//可以根据需要返回html}请在相关的component.ts文件中为getDivHtml添加适当的实现。
对于Angular 1.x,在html中使用ng bind html:
<div ng-bind-html="thisCanBeusedInsideNgBindHtml"></div>
此时,您将尝试在安全上下文错误中使用不安全的值,因此需要使用ngSanitize或$sce来解决此问题。
$sce
在控制器中使用$scetrustAsHtml()转换html字符串。
$scope.thisCanBeusedInsideNgBindHtml = $sce.trustAsHtml(someHtmlVar);
ngSanitize公司
有两个步骤:
包括angular-cleanity.min.js资源,即:<script src=“lib/aangular/aangular cleanity.min.js”></script>在js文件(controller或通常是app.js)中,包含ngSanitize,即:angular.module('myApp',['myApp.filters','myApp.services',“myApp.指令”,“ngSanitize”])
Angular JS在标记中显示HTML
上面的链接中提供的解决方案对我有效,但这个线程上的选项都没有。对于任何希望使用AngularJS版本1.2.9实现相同功能的人
这是一份副本:
好的,我找到了解决方案:JS文件:$scope.renderHtml=函数(html_code){返回$scetrustAsHtml(html_code);};HTML格式:<p ng bind html=“renderHtml(value.button)”></p>
编辑:
以下是设置:
JS文件:
angular.module('MyModule').controller('MyController', ['$scope', '$http', '$sce',
function ($scope, $http, $sce) {
$scope.renderHtml = function (htmlCode) {
return $sce.trustAsHtml(htmlCode);
};
$scope.body = '<div style="width:200px; height:200px; border:1px solid blue;"></div>';
}]);
HTML文件:
<div ng-controller="MyController">
<div ng-bind-html="renderHtml(body)"></div>
</div>
对于这个问题,还有一种解决方法,即在angular中创建新的属性或指令。
产品规范.html
<h4>Specs</h4>
<ul class="list-unstyled">
<li>
<strong>Shine</strong>
: {{product.shine}}</li>
<li>
<strong>Faces</strong>
: {{product.faces}}</li>
<li>
<strong>Rarity</strong>
: {{product.rarity}}</li>
<li>
<strong>Color</strong>
: {{product.color}}</li>
</ul>
应用程序.js
(function() {
var app = angular.module('gemStore', []);
app.directive(" <div ng-show="tab.isSet(2)" product-specs>", function() {
return {
restrict: 'E',
templateUrl: "product-specs.html"
};
});
索引html
<div>
<product-specs> </product-specs>//it will load product-specs.html file here.
</div>
or
<div product-specs>//it will add product-specs.html file
or
<div ng-include="product-description.html"></div>
https://docs.angularjs.org/guide/directive