0

Where I want the html to go:

<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>

How could I pass the html to the js file?

UPDATE: I can't use ng-include because this is a SPA, so the area where the contact.html will go isn't static.

4
  • create an angular service which provides your contact html data Commented Oct 30, 2015 at 14:11
  • I don't know how to do this, I'm new to angularJS. I have seen people mention services, but I do not know how to use them Commented Oct 30, 2015 at 14:12
  • Can't you simply use ngInclude? Commented Oct 30, 2015 at 14:14
  • No, the area where this contact form is changes, building a SPA. So I can't use the ng-include Commented Oct 30, 2015 at 14:19

2 Answers 2

1

You'll need to use the $sce service and ng-sanitize. Once you have the HTML you want to include in your controller/directive, use :

$sce.trustAsHtml($scope.htmlItem);

And in your view:

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

And the HTML will be rendered onto the page.

Sign up to request clarification or add additional context in comments.

3 Comments

This is something I was looking for, but how would I get the contact.html into the $scope?
make request :D forgot, how it in angular, but something like this (it's bad practice for angular, use angular http service): $.ajax({url: '/contact.html', success: function(data){ $scope.display = data; $scope.$apply(); }})
How long is the content you are wanting to toggle around and under what conditions?
0

Alternativelly, you can you ng-include.

    <!-- 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>

Remember to use nested " ' ' " for Angular to understand.

You should also take a look at $RouteProvider.

This is a example of mine, showing how to use RouteProvider to populate ng-view. You can set the view, controllers and initial variables on the routeProvider.

5 Comments

I can't use this because the data on the page isn't static, it will change when the user interacts with the page
Edited my answer. Look at my example code. The views/partials aren't static and don't need to be. This page, for example, shows exactly that.
@DanielMcCrevan Take a look at $routeProvider, I think that this is exactly what you are looking for.
I see what you are doing here, but I don't think this is what I need. I just need a simple way to get the HTML into a $scope variable, would it be fine for me to just put the HTML as a string in a variable?
When I was beginning with Angular, I did used plain strings in scope variables but I don't remember how to apply then. Let me test a sec.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.