javascript - AngularJS 将 HTML 传递到作用域

标签 javascript html angularjs

我想要 html 去哪里:

<div>{{display}}</div>

app.js

var app = angular.module('app', []);
app.controller('ctrl', function ($scope) {
     $scope.contact = //get the html from contact.html
     $scope.display = $scope.contact
});

联系.html

<form>
  ...
</form>

如何将 html 传递到 js 文件?

更新:我无法使用 ng-include,因为这是一个 SPA,因此 contact.html 所在的区域不是静态的。

最佳答案

您需要使用$sce服务和ng-sanitize 。一旦您获得了要包含在 Controller /指令中的 HTML,请使用:

$sce.trustAsHtml($scope.htmlItem);

在您看来:

<div ng-bind-html="htmlItem"></div>

HTML 将呈现到页面上。

关于javascript - AngularJS 将 HTML 传递到作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33437989/

相关文章:

javascript - 倒计时完成后清除(本地存储、 session 存储)?

javascript - 为什么 onSubmit 中的方法调用会抛出错误?

javascript - jQuery:用其他元素替换元素?

html - 坚持使用 Bootstrap 的列

javascript - 使用 jQuery 将类添加到父 div 之外的此元素

javascript - AngularJS 中 ng-include 内的范围丢失

javascript - 当使用服务和 API 登录以在 MySQL 中获取时,Angularjs 在 $scope 中显示特定用户的动态 ID 和信息

angularjs - 将 AngularJS 嵌套表单设置为已提交

javascript - 使用 NodeJS(或 PHP)将 API 数据保存到 JSON 文件

javascript - 使用 javascript 将 HTML 表格数据存储在数组中