Note that there are some explanatory texts on larger screens.

plurals
  1. POInserting content into a specific transcluded element (Angular)
    primarykey
    data
    text
    <p>I am trying to create a directive to create multiple choice activies. The idea is that in my scope I have a model with an array of questions as such:</p> <pre><code>function Ctrl2($scope) { $scope.activity1 = { "questions": [ { "question": "This is the first question", "answers": ["answer1", "answer2"] }, { "question": "When was the American revolution?", "answers": [1776, 1881, 1778] } ] }; } </code></pre> <p>I have a directive which finds the questions from the scope and then appends it to the directive element. Here is my directive (simplified for clarity)</p> <pre><code>.directive('multipleChoice', function(){ return{ templateUrl: "&lt;div class='ng-transclude'&gt;&lt;/div&gt;", scope: { activity:'=' }, replace: true, transclude: true, link: function(scope, element, attrs){ for (var i=0;i&lt;scope.activity.questions.length;i++){ var question=scope.activity.questions[i]; element.append(question.question); } element.append("&lt;button ng-submit='submit()'&gt;Submit&lt;/button&gt;&lt;button&gt;Show Solutions&lt;/button&gt;") } } }) </code></pre> <p>However, sometimes I want to insert HTML content between the questions. The way I envisioned doing it something like this. In my HTML</p> <pre><code> &lt;div multiple-choice activity="activity1"&gt; &lt;div id="question1"&gt;&lt;/div&gt; &lt;img src=question2.jpg/&gt; &lt;/div&gt; </code></pre> <p>and then im my directive instead of appending it to the directive element, appending it to the element with id="question1".</p> <p>I know an easy solution would be to use JQuery with: 1- rework question model to:</p> <pre><code>{ "question": "This is the first question", "answers": ["answer1", "answer2"], "ctn": "#question1" } </code></pre> <p>and then in my directive doing</p> <pre><code>if (question.ctn!==undefined){ $(ctn).append(question.question); } else{ element.append(question.question) } </code></pre> <p>However, I want to stay away from JQuery and try to the "Angular Way".</p> <p>Another solution I can think of would be to have something like this in my HTML:</p> <pre><code> &lt;div multiple-choice activity="activity1"&gt; &lt;div&gt;{{activity.questions.question[0]}}&lt;/div&gt; &lt;img src=question2.jpg/&gt; &lt;/div&gt; </code></pre> <p>However, I'm not sure how that would work.</p> <p>What do you think? Do I have to use jQuery or is there a better way?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload