Search code examples
javascripthtmlangularjsangularjs-scope

I want to use a scope variable as part of my javascript's onfocus function's argument


<input ng-show="showFolderNameInput" 
       type="text" 
       name="folderName" 
       ng-model="folderName" 
       style="margin-left: 5%; width: 90%;padding-left:5px;" 
       placeholder="{{enterFolderName}}" 
       autofocus/>

Here is some HTML, I want to add a javascript handler onfocus=setSelectionRange(aaa,bbb), and I want its arguments "aaa" and "bbb" to be a scope variable.


Solution

  • You need to think in a more "angularjs" way:

    simply use ng-focus:

    <input ng-show="showFolderNameInput" 
       type="text" 
       name="folderName" 
       ng-model="folderName" 
       style="margin-left: 5%; width: 90%;padding-left:5px;" 
       placeholder="{{enterFolderName}}" 
       autofocus
       ng-focus="focused(aaa,bbb)"/>
    

    and in your controller, create the listener:

    $scope.focused = function(aaa, bbb){
      console.log(aaa, bbb);
    }
    

    This way, you'll be able to handle all your javascript in your controller: this will let you to have a more organized and maintainable code.