2015-01-12 10 views
5

supponga abbiamo bisogno di creare due modi per definire configurazioni di una direttiva:come richiede una direttiva (direttiva all'interno di un altro) solo se esistono

1 - utilizzando l 'elemento attributi

<any main-dir main-name="myname" name-id="may-id" main-other-config="other config"></any> 

2- che richiede un altro direttiva

app.directive("mainDirConfig", function() { 
    return { 
    controller: function (scope){ 
     scope.config = { 
     name: 'my name', 
     id: 'my-id', 
     otherConfigs: 'other config' 
     } 
    } 
    } 
}); 


<any main-dir main-dir-config></any> 

come richiedere la direttiva mainDirConfig all'interno della direttiva mainDir (come metodo preferito) solo se mainDirConfig esiste e in caso contrario utilizzare gli attributi elemento come configurazioni?

Altre informazioni: Voglio utilizzare questa configurazione per un modulo esterno e ho bisogno di separare le configurazioni utente dal modulo.

+0

richiede: "? OptionalParentDirective" – setec

+1

@setec Buono! grazie. per favore aggiungi come risposta in modo che io possa contrassegnarlo. – Reyraa

risposta

1

Dalla documentazione angolare here:

richiedono

Require another directive and inject its controller as the fourth argument to the linking function. The require takes a string name (or array of strings) of the directive(s) to pass in. If an array is used, the injected argument will be an array in corresponding order. If no such directive can be found, or if the directive does not have a controller, then an error is raised. The name can be prefixed with: 

(no prefix) - Locate the required controller on the current element. Throw an error if not found. 
? - Attempt to locate the required controller or pass null to the link fn if not found. 
^ - Locate the required controller by searching the element and its parents. Throw an error if not found. 
^^ - Locate the required controller by searching the element's parents. Throw an error if not found. 
?^ - Attempt to locate the required controller by searching the element and its parents or pass null to the link fn if not found. 
?^^ - Attempt to locate the required controller by searching the element's parents, or pass null to the link fn if not found. 

E 'menzionato nella sezione direttiva, ma solo completamente definito nella sezione $ compilazione.

Problemi correlati