Javascript ng bind html无法解析正确的html并将其绑定
我的控制器如下所示:Javascript ng bind html无法解析正确的html并将其绑定,javascript,html,angularjs,Javascript,Html,Angularjs,我的控制器如下所示: myApp.controller('actionEditController', ['$scope', '$stateParams', '$sce',function ($scope, $stateParams, $sce) { $scope.table="<p>OOPSY</p>"; $scope.test="<p>TEST</p>"; $.get(apiHost
myApp.controller('actionEditController', ['$scope', '$stateParams', '$sce',function ($scope, $stateParams, $sce) {
$scope.table="<p>OOPSY</p>";
$scope.test="<p>TEST</p>";
$.get(apiHost + "/action/entity/" + $stateParams.id)
.success(function (response) {
var action = response['RESPONSE'];
action.params = JSON.parse(action.paramsJson);
$scope.$apply(function () {
$scope.table = $.hbs("/templates/action.hbs", action);
});
console.log(" table is "+$scope.table);
})
.error(function (jqXHR, textStatus, errorThrown) {
console.error("Error retrieving action");
});
}]);
console.logtable的输出为:
<div>
<input name="name" type="text" value="EXPIRY_SM00004_BROWSED"/>
<input name="id" type="text" readonly="readonly" value="AC0000E1"/>
<input name="type" type="text" readonly="readonly" value="SCHEDULE"/>
<input name="priority" type="text" value="LOW"/>
<input name="eventName" type="text" value="expiry.SM00004.browsed"/>
<input name="space" type="text" value="hybrid"/>
<input name="at" type="number" value=59400000/>
<input name="on" type="number" value=172800000/>
</div>
<script src="lib/angular/angular-sanitize.min.js"></script>
我的html文件:
<div ng-app="broConsoleApp" ng-controller="actionEditController" >
<h2 class="marcellus text-center"> EDIT ACTION </h2>
<div ng-bind-html="table">
</div>
<div ng-bind-html="test"></div>
</div>
正在打印测试,但未打印表格。我想是因为输出中出现了一些新行字符?我尝试用以下内容替换它们:
$scope.table.replace/\n/g,
然而,我仍然没有得到表的输出。有人请帮我格式化这个渲染输出,以便ng bind可以绑定它。我认为html在特殊字符等方面没有任何问题。我开始认为ngBindHtml根本无法处理这个问题 请检查plunker: 我使用了并修改了它,只在html中添加了一个输入按钮,它不会呈现
$scope.myHTML =
'I am an <code>HTML</code>string with ' +
'<a href="#">links!</a> and other <em>stuff</em>' +
'<input name="name" type="text" value="EXPIRY_SM00004_BROWSED"/>';
它似乎与$sce.trustAsHtml一起工作: 视图: js:
工作示例:我认为您需要将“ngSanitize”添加到模块列表中。我已经添加了该依赖项。这就是为什么要打印测试。你不需要在html上调用$sce.trustAsHtml吗?我也尝试过。我想直接给它调用$sanitize的html,否则它不会。无论如何,我也尝试了$sce.trustAsHtml:\但是如果我们不明确信任ashtml,它不应该只是清理并返回html吗?
<div ng-app="app" ng-controller="Ctrl">
<div ng-bind-html="table"></div>
<div ng-bind-html="test"></div>
</div>
angular.module('app', [])
.controller('Ctrl', function($scope, $sce) {
$scope.table = $sce.trustAsHtml('<p>this is a test</p>');
$scope.test = $sce.trustAsHtml('<h3>TEST</h3>');
});