2017-12-21 9 views
0

divにはng-clickがあります。そのdivをクリックすると、ディレクティブからスクリプトコンテンツを取得する関数が呼び出され、別のdivにそのスクリプトを追加し、スクリプトのコンテンツにアクセスします。しかし、私がディレクティブの内容を取得するとき、私はディレクティブにコンテンツの名前をつけていません。私は内容を取得したい。角度JS:Javaスクリプト機能で指示内容を使用できません

私が呼ぶ機能:

$scope.someFunction = function(){ 
    var appendHtml = $compile("<my-custom-directive></my-custom-directive>")($scope); 
    $("#someId").append(appendHtml) 
    //But when i append I am seeing as <my-custom-directive></my-custom-directive> in html not the actual content 

    $(""#someId"").find('script') 
} 

指令:

app.directive('myCustomDirective', function ($compile) { 
    return { 
    restrict: 'E', 
    templateUrl: '/somecontent.html', 
    replace: true, 
    link: function ($scope, elem, attr, ctrl) {} 
    }; 
}); 

Somecontent.html

<script type="text/template"> 
    <div class="arrow" style="left: 50%;"></div> 
    some elements here 
    </div> 
</script> 

Iから呼び出すHTML:

<div ng-click="someFunction()"> 
    <div id="someId"> 
    <my-custom-directive></my-custom-directive> 
    //But Here I am seeing this, when calling 
    $(appendHtml).find('script') in my javascript function, after Javasciprt function call is done, It works fine. But i want to see actual content here when calling $(""#someId"").find('script') 
    <div> 
</div> 

答えて

0

それは良い習慣ではありません。

HTML

<div ng-click="someFunction()"> 
    <div id="someId"> 
    <div ng-if="$scope.isVisible"> 
      <my-custom-directive></my-custom-directive> 
    </div> 
    //But Here I am seeing this, when calling 
    $(appendHtml).find('script') in my javascript function, after Javasciprt function call is done, It works fine. But i want to see actual content here when calling $(""#someId"").find('script') 
    <div> 
</div> 

コントローラ:あなたはNG-場合は、代わりに結合、follwingのように使用することができます

$scope.isVisible = false; 

$scope.someFunction = function(){ 
    $scope.isVisible = true; 
} 

また、あなたのディレクティブにスコープのparamを隔離渡すことができますし、ディレクティブテンプレートのparamを確認してください

+0

someFunctionが呼び出されたときに、必要に応じてコンテンツを取得しています。だから私の状況ではあなたのソルーションはうまくいかない。 – emar

0

あなたはちょうどn jQueryまたはjqLit​​eを使用して要素を正しく選択する

あなたsomeFunctionはこのように多くを見るために必要がある場合があります。

vm.someFunction = function() { 
    var appendHtml = $compile('<my-custom-directive></my-custom-directive')($scope); 
    angular.element(document).find('some-id-element').append(appendHtml); 
}; 

私は一緒に私はあなたが何をしようとして達成かもしれないと思うthis plunkを置きます。

これは目標に近似していますか?

+0

お返事ありがとうございます。しかし、私は同じJavaスクリプト関数内で追加されたコンテンツを使用する必要があります。たとえば、私は同じ関数で追加されたスクリプトを使用したいと思います。同様に、angle.element(document).find( 'some-id-element')。find( 'script')。私がそのディレクティブを追加した後でさらに追加するには、そのディレクティブをポップアップする必要があります。 – emar

関連する問題