2017-01-22 3 views
0

image-multiselectというanglejsディレクティブを作成しました。これは次のように使用できます。Angularjsディレクティブ:String to directiveとして渡されたhtmlがテンプレートにレンダリングされない

<image-multiselect items="landTransportTypes" image="illustrationURL" itemname="name" append="<div class='detail'>...some html here...</div>"></image-multiselect> 

append属性には、文字列としてhtmlが割り当てられていることに注意してください。私は

function(){ 

    var imageMultiselect = function(){ 

     return { 

      scope : { 
         items: "=", 
         image: "@", 
         itemname: "@", 
         append: "@" 

        }, 

      template : "<div style='background:#f2f2f2;padding:20px;margin:20px;border-radius:5px;min-height:100px;'>" + 
         "<div ng-repeat='item in items' class='card text-center'>" + 
          "<img class='card-img' src='{{item[image]}}' alt='Avatar'>" + 
          "<div class='detail'>" + 

           "<b>{{item[itemname]}}</b>" + 

           /*append used to customize template as per requirement*/ 
           "{{append}}"+ 

          "</div>" + 
         "</div>" + 
         "</div>" 

     } 


    } 

    angular.module("myApp").directive("imageMultiselect",imageMultiselect); 

}()); 

問題を次のようにDDOにtemplate属性を変更するために使用することを期待このHTML文字列:APPENDに渡されたHTML文字列は、全体ではなく、マークアップは、それがページ上にあるように表示されるHTMLとしてレンダリングされていませんか?

+0

使用NG-バインドによってng-html-compileディレクティブを使用しました-html、else評価されていない –

答えて

0

@アンダーあなたの応答に感謝、それは私に正しい方向を与えました。私はAnderのアプローチ@使用ではなくng-bind-htmlを使用して、私はフランシス・ブーヴィエ(https://github.com/francisbouvier/ng_html_compile

利用指令

<image-multiselect items="landTransportTypes" 
            image="illustrationURL" 
            itemname="name" 
            append="<input type='text' name='count' placeholder='Count' ng-model="transport.count" class='form-control input-sm'/></div>"> 
            </image-multiselect> 

指令の定義

(function(){ 

    var imageMultiselect = function(){ 

     return { 

      scope : { 
         items: "=", 
         image: "@", 
         itemname: "@", 
         append: "@" 

        }, 

      template : "<div style='background:#f2f2f2;padding:20px;margin:20px;border-radius:5px;min-height:100px;'>" + 
         "<div ng-repeat='item in items' class='card text-center'>" + 
          "<img class='card-img' src='{{item[image]}}' alt='Avatar'>" + 
          "<div class='detail'>" + 
           "<b>{{item[itemname]}}</b>" + 
           /* This is where i use ng-html-compile directive, which works wonders for me, refer : https://github.com/francisbouvier/ng_html_compile */ 
            "<span ng-html-compile='append'></span>" + 

          "</div>" + 
         "</div>" + 
         "</div>" 

     } 


    } 

    angular.module("myApp").directive("imageMultiselect",imageMultiselect); 


}()); 
1

未知のHTMLを危険にさらす可能性があるため、角度はHTMLをレンダリングしません。あなたはHTMLを使用ngSanitize
をレンダリングしたい場合は$のサニタイズサービスを使用する場合は次に

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

を使用しています。 ng-bind-htmlのデータは、デフォルトでレンダリングされます。

私はあなたのexsampleにplunkerを作りました:https://plnkr.co/edit/Vi46BsuAsnuk3N3R4Yz9?p=preview

変更はappend varibaleはNG-バインドHTML、sanitaizeがsciriptタグにダウンロードされてバインドされ、モジュール内に注入されていることです。

+0

ありがとうございました。私が探していたものですが、唯一の質問です帽子は今では 'ng-bind-html'に与えられたHTMLマークアップの中で' ng-model'を使うことができます。 – RaghaveShukla

+0

何をしようとしているのか分かりませんが、Stackoverflowの入力とプレビューのようなものなら、この投稿のアイデア:http://stackoverflow.com/questions/20796102/angularjs-data-bind-in-ng-bind-html説明するために単純なプランナーを作った:https://plnkr.co/edit/SXIIdIVbDlmAqMnjKITJ? p = preview –

+0

私は何をしようとしているのは、画像のマルチセレクトギャラリーを作成することです。これは、画像の名前とアイテムの名前を表示するURLのリストを取得しますが、空のままである詳細セクションも持ちます。開発者がいくつかのhtmlを提供すると、その詳細セクションはそのhtmlに置き換えられます。私たちのアプリケーションでは、場合によっては詳細のセクションを項目数の格納に使用する入力フィールドに置き換える必要があります。しかし、私は誰もがそれを使用できるように汎用的にする予定です。私が経験しているのは、あなたの例ではng-bind-htmlは単純なhtmlを取るだけですが、入力フィールドは削除されます。 – RaghaveShukla

関連する問題