2016-07-16 5 views
0

Ich mache Textfelder mit einer Direktive und wollen nur das erste Textfeld im Fokus sein. Ich verwende eine andere Direktive für den Fokus. Dies ist das Skript:Fokussierung nicht auf Textfeld durch Winkel Direktive angewendet

<script> 
angular.module('MyApp',[]); 
angular.module('MyApp').controller('myController',myController); 
angular.module('MyApp').directive('myDirective',myDirective); 

angular.module('MyApp').directive('textBoxDirective',textBoxDirective); 

function myController() 
{ 

} 



function myDirective() 
{ 
    return{ 

     restrict: 'A', 
     scope:{ 
       abcd:'=' 
     }, 
      link : function(scope, $element) { 
       console.log(""+scope.abcd); 
       if(scope.abcd=='true'){ 
        $element[0].focus(); 
       } 


       } 
    }; 
} 

function textBoxDirective() 
{ 
    return{ 
     scope:{ 
      efgh:'@' 
     }, 
     restrict: 'A', 
     template: '<input type="text" data-my-directive="" data-abcd="efgh">' 

    }; 
} 

</script> 

Dies ist der html:

<body data-ng-app="MyApp" data-ng-controller="myController"> 

<div data-text-box-directive="" data-efgh="true"></div> 
<div data-text-box-directive=""></div> 
<div data-text-box-directive=""></div> 
</body> 

ich den Wert von abcd als wahr in myDirective einmal bekommen und undefined zweimal auf die Protokollierung, aber immer noch ist die Fokussierung auf die nicht angewendet erste Textbox Was mache ich hier falsch? Kann mir bitte jemand helfen?

Antwort

0
$timeout(function() { 
    if($element && $element[0]){ 
    $element[0].focus(); 
    } 
}); 
Verwandte Themen