2016-07-26 8 views
2

Ich möchte bedingte VorlageURL durch Attribut in meiner Direktive verwenden. hier ist mein Code:Angular bedingte VorlageURL

MapMarker.js:

app.directive('mapMarker',function(){ 
    return { 
'app/user/ngApp/templates/libsView/templates/directives/map.html', 
     templateUrl : function (elem , attrs) { 
      if (attrs["mapRequire"] == "yes") 
      { 
       console.info("have yes"); 
      } 
      else 
      { 

       console.info("doesn't have yes" , attrs["mapRequire"]); 
      } 
      return attrs["mapRequire"] == "yes" ? 
         'this is text' : '../templates/directives/map1.html'; 
     }, 

HTML:

<map-marker mapRequire = "yes"></map-marker> 

Es doesn't have yes undefined zurückzukehren.

Warum? Jeder Vorschlag für eine dynamische Vorlagen-URL hängt vom Attribut ab? Aber der Weg, ist es der beste Weg?

+0

ist damit verbundene Frage nach Richtlinie/Attribut Namenskonventionen http://stackoverflow.com/questions/33460274/angularjs-directive-naming-conventions/33460305#33460305 – dfsq

Antwort

2

Denken Sie an den Standard normalization conventions. Kebab-Fall in HTML übersetzt in CamelCase in JS.

Es sieht aus wie diese sollten:

<map-marker map-require="yes"></map-marker> 
-1

versuchen wählen-Richtlinie

statt bedingte url.

1

alle HTML-Attribute in Schlange Fall sein (nur Kleinbuchstaben und ‚-‘ erlaubt ist) ..

und Sie werden mit dem mapRequire Attribut in Kamel Kasusform

Stattdessen sollten Sie es verwenden, Dies als

<map-marker map-require = "yes"></map-marker>