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

  • Home
  • SEARCH
  • 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 8649109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:28:40+00:00 2026-06-12T13:28:40+00:00

I have a scope variable $scope.first_unread_id which is defined in my controller. In my

  • 0

I have a scope variable $scope.first_unread_id which is defined in my controller. In my template, I have:

<div id="items" >
  <ul class="standard-list">
    <li ng-repeat="item in items" scroll-to-id="first_unread_id">
    <span class="content">{{ item.content }}</span>
    </li>
  </ul>
</div>

and my directive looks like:

angular.module('ScrollToId', []).
directive('scrollToId', function () {
  return function (scope, element, attributes) {
    var id = scope.$parent[attributes["scrollToId"]];
    if (id === scope.item.id) {
      setTimeout(function () {
        window.scrollTo(0, element[0].offsetTop - 100)
      }, 20);
    }
  }

});

it works, however, two questions:

  1. Is there a better way of getting the “first_unread_id” off the controller scope into the direct than interrogating scope.$parent? This seems a bit ‘icky’. I was hoping I could pass that through the view to the direct as a parameter w/o having to repeat that on ever li element.

  2. Is there a better way to avoid the need of the setTimeout() call? Without it, it works sometimes – I imagine due to difference in timing of layout. I understand the syntax I have used is defining a link function – but it isn’t clear to me if that is a pre or post-link by default – and if that even matters for my issue.

  • 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-12T13:28:41+00:00Added an answer on June 12, 2026 at 1:28 pm
    1. You shouldn’t need the scope.$parent – since it will inherit the value from the parent scope, and when it changes in the parent scope it will be passed down.
    2. The default is a post-link function. Do you have some images or something loading that would make the page layout change shortly after initial load? Have you tried a setTimeout with no time on it, eg setTimeout(function(){})? This would make sure this would go ‘one after’ everything else is done.
    3. I would also change the logic of your directive a bit to make it more general. I would make it scroll to the element if a given condition is true.

    Here are those 3 changes:

    html:

    <div id="items" >
      <ul class="standard-list">
        <li ng-repeat="item in items" scroll-if="item.id == first_unread_id">
          <span class="content">{{ item.content }}</span>
        </li>
      </ul>
    </div>
    

    JS:

    app.directive('scrollIf', function () {
      return function (scope, element, attributes) {
        setTimeout(function () {
          if (scope.$eval(attributes.scrollIf)) {
            window.scrollTo(0, element[0].offsetTop - 100)
          }
        });
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page-scoped component, which has an instance variable List with data, which
I have a question about variable scope in TCL, I have the following code:
unordered_map<std::string,unordered_map<std::string, std::string> >* storing_vars; I have this variable in the scope declared in the
I have this scope: scope :user_id, :as => user do resources :boards, :controller =>
I have the following scope, which I know is not optimal: scope :event_stream_for, lambda{
I have a question about best practices for Ruby variable-scope using class_eval . In
I have a set of enum values defined in the Foo class (below). namespace
Possible Duplicate: Child Scope & CS0136 C# Variable Scoping Though I have been using
I have a controller which I use for a login form. In the view,
I have one of those pesky variable scope problems with $.post. Before submitting a

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.