0

私はAngularが初めてで、新しい要素を動的に作成する方法を理解しようとしています. ペインタグをテンプレートに置き換える「ペイン」という要素ディレクティブがあります。

<pane ng-controller="AreaCtrl"></pane>

ペインのテンプレート (要約):

<div>
    <section></section>
    <div class="subs"></div>
</div>

section タグ内の項目をクリックすると、クリックした項目の「ペイン」がまだ存在しない場合、class="subs" を使用して div 内に別の「ペイン」を追加したいと考えています。追加されたペイン タグを取得してディレクティブを呼び出し、テンプレートに置き換えることができません。Angular の初心者にとっては、正しい方向へのポイントはどれでも素晴らしいことです。

ここに簡単な jsfiddle があります: http://jsfiddle.net/n9vXz/1/

4

2 に答える 2

0

I'm not sure how you're appending it to the body, but for showing you how to do it I'll assume it's just an element you have.

var newPaneElement = .....
someOtherElement.append(newPaneElement);

In order for your directive to be processed now, you'll have to tell it to $compile

You can do this by running the following directly after appending it to someOtherElement:

$compile(newPaneElement)($scope OR scope);

I put $scope or scope because I'm not sure if where you're appending it is in a controller, or within a directive.

If you can paste a jsFiddle that would be helpful.


There's no need to do compile, I think you should watch some of the tutorial videos on how to use angular. Looking at the jsfiddle you have, you don't need jquery. Also you have a directive wiring up on the pane element, but then within that directive you have another pane element so it was doing a recursive loop and crashing. I've fixed the fiddle up to do what you need:

JsFiddle http://jsfiddle.net/BcvPz/1/


Use an ng-repeat and then you can just have pane elements repeat wherever

JsFiddle http://jsfiddle.net/BcvPz/2/

于 2013-05-02T19:35:07.640 に答える