3

I'm using AngularJS to design a small app for searching. I have a div that's currently empty, and after running a function, for it to replace it with a div that has ng-include. The div replaces just fine, but ng-include doesn't load up.

I'm currently running the following in console for testing to see get it running. How would I go about getting this to work? Any help is appreciated!

document.getElementById("resultsDiv").innerHTML = '<div ng-include="" src=" \'sr.html\' "></div>';

3 Answers 3

2

read about angularjs $compile function

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

1 Comment

Took a couple reads though it, but I don't see a working method of compiling after clicking a button (which will run through my function and compile it last). Would you happen to have an example?
1

I don't know why you need to make this JavaScript-call, but its definitely no the 'angular-way'. If you need to conditionally include html, i would recommend using ng-if, ng-hide, ng-show or even ng-switch.

You could do something like this:

// somewhere in your controller 
$scope.triggerSomeInclude = true;

// in your template
<div ng-if="triggerSomeInclude">
    <div ng-include ="'someFile.html'"></div>
</div>

Another approach would be using a directive. They are the only place, where selecting an element directly could make sense (although even there it usually doesn't to select an element via id). But as I said, it's hard to stay what the best method would be, as I'm not sure what you're trying to achieve.

Although you're not using jQuery, what you're trying to do looks very jQueryesque (awful word) as you're selecting an element directly seemingly totally detached from the $digest and $compile-cycles of angular, so I also would recommend to read this answer: "Thinking in AngularJS" if I have a jQuery background?

Comments

1

In the end, the method I used was templating example used for ngInclude.

http://docs.angularjs.org/api/ng.directive:ngInclude

Inside my controller, for example, by default it would set the following:

$scope.mainDivTemplate = {url: 'partials/test1.html'}

If I wanted to switch it to something else, I'd call a function that would look a little something like this.

$scope.loadHome = ->
    $scope.mainDivTemplate = {url: 'partials/home.html'}
    $scope.$apply()

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.