AngularJS将HTML传递给范围

时间:2015-10-30 14:10:03

标签: 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
});

contact.html

<form>
  ...
</form>

我怎样才能将html传递给js文件?

更新:我不能使用ng-include,因为这是一个SPA,所以contact.html将去的区域不是静态的。

2 个答案:

答案 0 :(得分:1)

您需要使用$sce服务和ng-sanitize。获得要包含在控制器/指令中的HTML后,请使用:

$sce.trustAsHtml($scope.htmlItem);

在你看来:

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

HTML将呈现在页面上。

答案 1 :(得分:0)

或者,您可以选择包含。

    <!-- HEADER -->
    <div ng-include="'partials/header.html'"> </div>

    <!-- MAIN VIEW -->
    <div animated-view></div> //This is using ngRoute to populate content.

    <!-- FOOTER -->
    <div ng-include="'partials/footer.html'"> </div>

请记住使用嵌套的“''”来让Angular理解。

您还应该查看$RouteProvider

这是example of mine,显示如何使用RouteProvider填充ng-view。您可以在routeProvider上设置视图,控制器和初始变量。