Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8794977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:19:28+00:00 2026-06-13T23:19:28+00:00

I am using a directive which modifies the DOM and therefore must be done

  • 0

I am using a directive which modifies the DOM and therefore must be done after all postLink fns are called. (see angular-ui modules/directives/select2/select2.js:103)

Given certain preconditions (not encoded in the URL), I would like to set the focus to the first select2 element on the page. This is problematic because before setting focus, select2 must be initialized, and without having some cheesy arbitrary delay > 0, I can’t schedule an event to be broadcasted that will set the focus.

I have created the following directive which will listen for a focus primaryInput/focus event and set the focus.

# TODO - should we create a directive for tab order instead?
app.directive "primaryInput", () ->
  {
    name: "primaryInput"
    link: (scope, element, attrs) ->
      scope.$on "primaryInput/focus", ->
        element.focus()
  }

Changing the primaryInput directive priority does not work, as any priority will run before the setTimeout function is scheduled.

Ideally, I’d like to be able to have a collection of promises which each delayed directive pushes to a stack, and then a promise which gets resolved once all of those promises are complete. I would have focused my question on how to best achieve this, but I can think of a not-very-good way already, would like to explore better ways, and want to avoid the XY problem.

Thank you!

Update

After going through the effort of describing the problem here, taking a step back, I thought of an acceptable approach to my problem and will answer my own question, but leave it open if anyone has a better approach.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:19:29+00:00Added an answer on June 13, 2026 at 11:19 pm

    Since the delayed execution complexity is in the directive dealing with Select2, I decided, for now, to isolate that complexity there.

    I override the focus function on the element in my Select2 directive, so focus calls are forwarded to the select2 instance once it is available. This way, the element can be told to focus before select2.

    My focus directive:

    app.directive "input", ($injector) ->
      primaryMatcher = (e,attrs) ->
        attrs.primary?
      {
        name: "inputFocus"
        restrict: 'E'
        link: (scope, element, attrs) ->
          scope.$on "focus", (e, matcher)->
            if ((matcher || primaryMatcher)(element, attrs))
              console.log("focus", element, attrs)
              element.focus()
      }
    

    In my Select2 directive:

    app.directive "s2Factor", () ->
      # ...
      {
        name: "s2Factor"
        require: "?ngModel"
        priority: 1
        restrict: "A"
        link: (scope, el, attr, controller) ->
          select2_p = $.Deferred()
          # ...
          setTimeout ->
            el.select2 opts
            select2_p.resolve(el.data("select2"))
    
          el.focus = () ->
            select2_p.then (select2) ->
              select2.focus()
      }
    

    It involves instance monkey patching, but I find it to be acceptable in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

what is the use of using declaration type of namespacing over using directive type
can cookies be blocked using a page directive instead of doing it programmatic?
Is there some sort of C# directive to use when using a development machine
I am using a generic error page using ASP.NET's <customErrors> directive. <customErrors mode=On defaultRedirect=500.html
Possible Duplicate: Should Usings be inside or outside the namespace sa1200 All using directives
I'm using a library with an include structure where the .h files are all
I am using JSON.NET which has the LinqBridge .dll merged in. LinqBridge allows Linq
From section 7.3.4.2 of the c++11 standard: A using-directive specifies that the names in
I'm using TCPListener which according to this page should be in System.Net.Sockets, I have
Sorry for this silly question, but is there any way to restrict using directives

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.